简体   繁体   中英

how to get details in html tags using preg_match_all

I'm using preg_match_all function to grab details on a web page. But problem is it returns exact matches to the given structure.

Example:

<span class="st">Details I want to get</span>

But actually it looks like:

<span class="st"><em>Details </em>I want to <b>get<b></span>

So it returns just a few results that exactly match the above given first code!

I found an article about grabbing & there was a code and is it possible to do this using a code like this because I found this working with above structure?

$nodes = $xPath->query('//span[@class="st"]');
foreach($nodes as $node) {
    echo $node->nodeValue, PHP_EOL;
}

Use php DOMDocument and strip_tags

$xml = '<span class="st">Details i want to get</span>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $span) {
 // you can check class etc with conditional statements
  #strip_tags to remove <em> and other tags inside that tag
  echo strip_tags($span->nodeValue), PHP_EOL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM