#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    14
    Rep Power
    0

    [PHP] Using regex to add bracketing code


    Hello, all...

    I'm having some difficulty with using preg_replace in PHP to find lines in a large block of HTML which do not contain a certain string and bracketing them with <em> tags.

    Basically, I've got a huge block of text sent from a form, which I did nl2br on to make it display with spaces.

    I now want to find anything that starts with two line breaks (<br /><br />) that then are *not* followed by a span tag, go all the way to the *next* set of line breaks, and surround the whole thing with <em></em>

    In other words:

    HTML Code:
    <br /><br />
    <span class="gold">Some varying text here.</span><br /><br />
    Some other text here, not wrapped in a span tag.<br /><br />


    I'd like to do a preg_replace which finds the second line (because it does not have the <span> tag) and makes it italic.

    Anyone have any thoughts on how to do this in PHP? Let me know if the question isn't clear or if you need more information. Thanks!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    1. The initial <br>s will have a line break after them. Use \s+ for that.
    2. A negative assertion can check if the bit after isn't a <span> tag; it looks like (?!..).
    3. An ungreedy dot-all for the rest, going up to a <br><br> or the end of the string.

IMN logo majestic logo threadwatch logo seochat tools logo