Find text between 2 HTML tags in PHP

A very good afternoon my exchangeios.

I have a question that is rather urgent, that I really hope you can help me with.

Let's say I have the following code in a string.

<span style="font-size:18px; line-height:normal"><span style="font-weight:bold">MOO
<br />
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
<td><span class="genmed"><strong>Code:</strong></span></td>
</tr>
<tr>
<td class="code">http&#58;//www.website.com/lalalal.php?p=moo</td></tr></table>
<br />


Now I want to be able to pull out that URL on the fly. So in an array I would just be left with any of the links that were inside the <td class="code">.

Please note that there will be quite a few links per page, however each of them start with the <td class="code"> and end with the </td>.

I am OK with you telling me how to do it, however if you could spare 5 mins and give me the code that would be fantastic!

thanks!
LVL 1
billy_howardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try:
preg_match_all('#([^<]+)#i',$htmlData, $matches);

print_r($matches[1]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cr4ck3rj4ckCommented:
Hope this helps,
CJ
$pattern = "/\<td class=\"code\"\>.*?\<\/td\>/";
preg_match_all($pattern, $str, $matches);
print_r($matches[0]);

Open in new window

0
darren-w-Commented:
Here is a function i've found on p2n, you would scan the file line by line and pass  it to this function

$start will be <td class="code">http&#58;
$end will be </td></tr></table>
$total will be "<td class="code">http&#58;//www.website.com/lalalal.php?p=moo</td></tr></table>"

This may be a problem if there is no spaces between the lines of code, in this case you will need to split the code up before passing it to the function
function my_strip($start,$end,$total){
 
$total = stristr($total,$start);
 
$f2 = stristr($total,$end);
 
return substr($total,strlen($start),-strlen($f2));
 
}

Open in new window

0
billy_howardAuthor Commented:
Fantastic. Exactly what I needed!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.