SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tricky eregi_replace...

    Ok, I admit it: regular expressions are not my favourite topic...

    Having said that, here's what I need to do. Given a string of text, I want to replace all instances of '<b ????>' (in other words, all "bold" tags that have attributes) with a simple '<b>' (just a simple bold tag). I was using
    PHP Code:
    $string eregi_replace('<b[^>.]*>','<b>',$string); 
    but then I noticed that '<br>' and '<br />' are being targetted also - which is something I don't want, of course.

    Any bright ideas out there?...

    Many thanks in anticipation.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $string preg_replace('/<b (.*?)>/i','<b>',$string); 

  3. #3
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    What happens if $string contains '<B>' instead of '<b>'?
    I thought - perhaps incorrectly - that preg_replace was case-sensitive...
    Thanks for the feedback.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the /i in the preg_replace will take care of that for you. It's for case insensitivity.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops... you're perfectly right. I had missed that Thanks for the input.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you were just missing a space (which was catching the <br>)

    PHP Code:
    eregi_replace('<b [^>]*>''<b>',  $string); 
    seems to work

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While '<b [^>]*>' will work in most cases, this is not a complete regexp for a html tag, because it's valid (or at least common) to have <> in a tag body, like "<b onclick='if(x>y)..". Friedl recommends something like the following:

    PHP Code:
    <<<HTML_TAG
            /
            <
            
    $tagName      # tag
            \b            # word break
            (             # repeat
                ".*?"     #   quoted value
                |         #   or
                '.*?'     #   quoted value
                |         #   or
                [^<>'"]+  #   attribute name or =
            )*            # 0 or more times
            >
            /ix
    HTML_TAG 

  8. #8
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now, that's something... Thank you for the comments also, as it makes it easier for regexp-deprived beings like myself...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •