Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

php - Get HTML tag containing string

I have a string let's say "John doe", I'm trying to figure out what HTML tag that contains that word.

I have a dom as a string and trying to match what tag contains that word. In this case, a span tag with class full-name

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>

        <h1>This is a Heading</h1>
        <p>This is a paragraph.</p>
        <span class="full-name">John doe</span>

    </body>
</html>

My code looks something like this so far, but do not get it to work, it just gets the entire DOM before the word

preg_match('/<(.*?)'.$name.'/s', $html, $match);
$matchBefore = $match[0];
preg_match('/'.$name.'(.*?)>/s', $html, $match);
$matchAfter = $match[0];
question from:https://stackoverflow.com/questions/66061310/get-html-tag-containing-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is the solution I came up with that works. Using XPath.

$homepage = file_get_contents("https://example.com"); // The page you are trying to scrape
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
@$doc->loadHTML($homepage);
$xPathDom = new DOMXpath($doc);

$query = "John Doe"; // String we search for

$results = $xPathDom->query("//*[normalize-space(text()) = '".$query."']");
$elements = [];
foreach($results as $element) {
    $elements[] = $element->textContent;
}
return $elements; // Contains all tags that contain $query or the search string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...