问题
In PHP I'm using the Simple HTML DOM Parser class.
I have a HTML file which has multiple A-tags.
Now I need to find the tag that has a certain text inside.
for example :
$html = "<a id='tag1'>A</a>
<a id='tag2'>B</a>
<a id='tag3'>C</a>
";
$dom = str_get_html($html);
$tag = $dom->find("a[plaintext=B]");
The above example doesn't work, since plaintext can only be used as an attribute.
Any idea's?
回答1:
<?php
include("simple_html_dom.php");
$html = "<a id='tag1'>A</a>
<a id='tag2'>B</a>
<a id='tag3'>C</a>
";
$dom = str_get_html($html);
$select = NULL;
foreach($dom->find('a') as $element) {
if ($element->innertext === "B") {
$select = $element;
break;
}
}
?>
回答2:
Assuming each specific text you are looking for maps only to a single link (which sounds like you do), you can build an associative lookup array. I just encountered this need myself. Here is how I handled it. This way you don't need to loop thru all the links every time.
function populateOutlines($htmlOutlines)
{
$marker = "courses";
$charSlashFwd = "/";
$outlines = array();
foreach ($htmlOutlines->find("a") as $element)
{
// filter links for ones with certain markers if required
if (strpos($element->href, $marker) !== false)
{
// construct the key the way you need it
$dir = explode($charSlashFwd, $element->href);
$code = preg_replace(
"/[^a-zA-Z0-9 ]/", "", strtoupper(
$dir[1]." ".$dir[2]));
// insert the lookup entry
$outlines[$code] = $element->href;
}
}
return $outlines;
}
// ...stuff...
$htmlOutlines = file_get_html($urlOutlines);
$outlines = populateOutlines($htmlOutlines);
// ...more stuff...
if (array_key_exists($code, $outlines)) {
$outline = $outlines[$code];
} else {
$outline = "n/a";
}
来源:https://stackoverflow.com/questions/11060170/php-simple-html-dom-parser-how-to-get-the-element-which-has-certain-content