1. hiding my <b> from ur <strong>
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Rep Power

    Matching inner text when outer text might match

    I want to match the span class "sample" and its contents in the following example string;

    <span><span class="sample"><i>This is great!</i></span></span>

    So I'm looking to match <span class="sample"><i>This is great!</i></span> but not the surrounding spans. The content inside this tag could be various, other than another </span>.

    Problem being, as you might have guessed, is that something like:
    /<span class="sample">.{1,100}<\/span>/

    will match:
    <span class="sample"><i>This is great!</i></span></span>
    instead of
    <span class="sample"><i>This is great!</i></span>

    I'm trying to indicate that I don't want the inner text not to match "</span>," which I'm guessing is the best way, but I'm having a hard time with it.

    Here's my latest attempt.

    $pattern = "/<span class=\"sample\">(^(?!.*(<\/span>)).{1,100})<\/span>/";

    Thanks for your help.

    Enjoy my post? Drop some props by hitting the scales button up top. JBL

    Website Design in Los Angeles and Washington, DC by PoweredPages.com
  2. #2
  3. Reversible Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Washington, USA
    Rep Power
    Don't use regular expressions for reading HTML. Wrong tool for the job.

    Load the HTML into something like DOMDocument, then grab a DOMXPath and do a search for that <span>. No regex and much simpler to understand.

    Comments on this post

    • Laurent_R agrees

IMN logo majestic logo threadwatch logo seochat tools logo