Mohammad reza Taheri Mohammad reza Taheri - 2 months ago 16
PHP Question

how to exract text between specific tags in html with php and regex?

I have searched in stackoverflow for my answer but can't find anything.

my question is how can I extract specific text between specific tags and if the first word of text find inside tag it grab it until the end

here is my code

$homepage = file_get_contents('http://www.time.com/');
$homepage=htmlentities($homepage);
echo $homepage;


for example it give me this output

<div>
<span class="12334d">first text in url</span>
<span id="asdasd2">second text in url</span> <br />
<span class="ddie8">third text in url</span>
<span>something else in url</span> <br/>
<span>special thing is here</span>
<span>something else in web</span>
</div>


so I want to grab "something else in url" and "something else in web" with regex when it find first word "something" grab text until the end between ... and put it in string.
thanks.

Answer Source

you can use the regex

[^>]*something[^<]*

see the regex demo