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('');
echo $homepage;

for example it give me this output

<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>

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.

Answer Source

you can use the regex


see the regex demo