[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find text between 2 HTML tags in PHP

Posted on 2008-11-18
4
Medium Priority
?
302 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:billy_howard
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22985607
try:
preg_match_all('#([^<]+)#i',$htmlData, $matches);

print_r($matches[1]);
0
 
LVL 6

Expert Comment

by:cr4ck3rj4ck
ID: 22985653
Hope this helps,
CJ
$pattern = "/\<td class=\"code\"\>.*?\<\/td\>/";
preg_match_all($pattern, $str, $matches);
print_r($matches[0]);

Open in new window

0
 
LVL 13

Expert Comment

by:darren-w-
ID: 22985728
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
 
LVL 1

Author Closing Comment

by:billy_howard
ID: 31517872
Fantastic. Exactly what I needed!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question