Espanta
Espanta

Reputation: 1140

How to differentiate tags in single Div when parsing HTML via simple_html_dom in PHP

I am using simple_html_dom in PHP to parse a live HTML page. But I got stock in the following situation. I have a div selector that has a nested span.

<div class="name1">Text1, has comma
<span class="name2">, Text2</span>
</div>

When I use the following code:

foreach($html->find('div[class=name1]') as $Results)
$text= $Results->xmltext;

Then I get a string of:

"Text1, has comma, Text2"

because both "Text1, has comma" and "text2" are part of div that has 'class=name1'.

What I need is to have separate texts of "Text1, has comma" and "Text2". I am able to extract "text2" using nested match out of it, but I cannot extract "text1" only.

Upvotes: 0

Views: 55

Answers (1)

Rodney G
Rodney G

Reputation: 4826

With $text1 = "Text1, has comma, Text2" and $text2 = ", Text2" you could use substr_replace():

$text1 = substr_replace($text1, '', -(strlen($text2)));

Upvotes: 1

Related Questions