Find text between 2 HTML tags in PHP

Posted on 2008-11-18
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">
<td><span class="genmed"><strong>Code:</strong></span></td>
<td class="code">http&#58;//</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!

Question by:billy_howard
    LVL 82

    Accepted Solution

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

    LVL 6

    Expert Comment

    Hope this helps,
    $pattern = "/\<td class=\"code\"\>.*?\<\/td\>/";
    preg_match_all($pattern, $str, $matches);

    Open in new window

    LVL 13

    Expert Comment

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

    LVL 1

    Author Closing Comment

    Fantastic. Exactly what I needed!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now