Php 从a获取文本(<;李>;
我有一些Php 从a获取文本(<;李>;,php,html,parsing,domparser,Php,Html,Parsing,Domparser,我有一些标签在内,如下所示: <li> <a href="link1"> one <li> <li> <a href="link2"> two <li> <li> <a href="link3"> three <li> <?php $html = '<li> <a href="link1"> one </a> <li> <li
标签在
内,如下所示:
<li> <a href="link1"> one <li>
<li> <a href="link2"> two <li>
<li> <a href="link3"> three <li>
<?php
$html = '<li> <a href="link1"> one </a> <li>
<li> <a href="link2"> two </a> <li>
<li> <a href="link3"> three </a> <li>
';
// Create a new DOM Document
$xml = new DOMDocument();
// Load the html contents into the DOM
$xml->loadHTML($html);
// Empty array to hold all links to return
$result = array();
//Loop through each <li> tag in the dom
foreach($xml->getElementsByTagName('li') as $li) {
//Loop through each <a> tag within the li, then extract the node value
foreach($li->getElementsByTagName('a') as $links){
$result[] = $links->nodeValue;
}
}
//Return the links
print_r($result);
/*
Array
(
[0] => one
[1] => two
[2] => three
)
*/
?>
one
两个
三个
如何使用HTML DOM解析器获取文本two
,然后将其放入数组中供以后使用?考虑使用来实现这一点。示例代码:
// include the simple html dom parser
include 'simple_html_dom.php';
// load the html with one of the sutiable methods available with it
$html = str_get_html('<li><a href="link1">one</a></li><li><a href="link2">two</a></li>');
// create a blank array to store the results
$items = array();
// loop through "li" elements and store the magic plaintext attribute value inside $items array
foreach( $html->find('li') as $li ) $items[] = $li->plaintext;
// this should output: Array ( [0] => one [1] => two )
print_r( $items );
//包括简单的html dom解析器
包括“simple_html_dom.php”;
//使用可用的可用方法之一加载html
$html=str_get_html('');
//创建一个空白数组来存储结果
$items=array();
//循环遍历“li”元素,并将神奇的明文属性值存储在$items数组中
foreach($html->find('li')as$li)$items[]=$li->明文;
//这应该输出:数组([0]=>1[1]=>2)
打印(项目);
考虑使用来实现这一点。示例代码:
// include the simple html dom parser
include 'simple_html_dom.php';
// load the html with one of the sutiable methods available with it
$html = str_get_html('<li><a href="link1">one</a></li><li><a href="link2">two</a></li>');
// create a blank array to store the results
$items = array();
// loop through "li" elements and store the magic plaintext attribute value inside $items array
foreach( $html->find('li') as $li ) $items[] = $li->plaintext;
// this should output: Array ( [0] => one [1] => two )
print_r( $items );
//包括简单的html dom解析器
包括“simple_html_dom.php”;
//使用可用的可用方法之一加载html
$html=str_get_html('');
//创建一个空白数组来存储结果
$items=array();
//循环遍历“li”元素,并将神奇的明文属性值存储在$items数组中
foreach($html->find('li')as$li)$items[]=$li->明文;
//这应该输出:数组([0]=>1[1]=>2)
打印(项目);
您需要确保a
标记已关闭,然后您可以这样做:
<li> <a href="link1"> one <li>
<li> <a href="link2"> two <li>
<li> <a href="link3"> three <li>
<?php
$html = '<li> <a href="link1"> one </a> <li>
<li> <a href="link2"> two </a> <li>
<li> <a href="link3"> three </a> <li>
';
// Create a new DOM Document
$xml = new DOMDocument();
// Load the html contents into the DOM
$xml->loadHTML($html);
// Empty array to hold all links to return
$result = array();
//Loop through each <li> tag in the dom
foreach($xml->getElementsByTagName('li') as $li) {
//Loop through each <a> tag within the li, then extract the node value
foreach($li->getElementsByTagName('a') as $links){
$result[] = $links->nodeValue;
}
}
//Return the links
print_r($result);
/*
Array
(
[0] => one
[1] => two
[2] => three
)
*/
?>
您需要确保a
标记已关闭,然后您可以这样做:
<li> <a href="link1"> one <li>
<li> <a href="link2"> two <li>
<li> <a href="link3"> three <li>
<?php
$html = '<li> <a href="link1"> one </a> <li>
<li> <a href="link2"> two </a> <li>
<li> <a href="link3"> three </a> <li>
';
// Create a new DOM Document
$xml = new DOMDocument();
// Load the html contents into the DOM
$xml->loadHTML($html);
// Empty array to hold all links to return
$result = array();
//Loop through each <li> tag in the dom
foreach($xml->getElementsByTagName('li') as $li) {
//Loop through each <a> tag within the li, then extract the node value
foreach($li->getElementsByTagName('a') as $links){
$result[] = $links->nodeValue;
}
}
//Return the links
print_r($result);
/*
Array
(
[0] => one
[1] => two
[2] => three
)
*/
?>
您想在PHP(服务器端)或javascript/JQuery(客户端)中这样做吗?您问题中的html有问题。。你有9个打开的标签,没有一个关闭的。这将很难按原样进行解析。您想在PHP(服务器端)或javascript/JQuery(客户端)中进行解析吗?您问题中的html有问题。。你有9个打开的标签,没有一个关闭的。这将很难按原样解析。谢谢,我刚刚对第二个FOREACH部分感到困惑,这很清楚,如果您按id获取第一个元素,您可以直接在a
上FOREACH。只是一个提示。谢谢,但是getelementbyid不适合我,因为@hakre说你可以直接搜索a
元素,只需使用foreach($li->getElementsByTagName('a')作为$links)
,getElementById
无法工作,因为没有指定id。谢谢,我只是对第二个FOREACH部分感到困惑,这是很清楚的。如果您按id获取第一个元素,您可以直接在a
上进行FOREACH。只是一个提示。谢谢,但是getelementbyid对我不起作用,因为@hakre说你可以直接搜索a
元素,只需使用foreach($li->getElementsByTagName('a')作为$links)
,getelementbyid
无法工作,因为没有指定id。