www.webdeveloper.com
Results 1 to 11 of 11

Thread: preg_replace - html tags

  1. #1
    Join Date
    Jan 2009
    Posts
    10

    preg_replace - html tags

    Hello,
    what regular expression I needed to use, if I want to change A to B, but I dont want to chage anything in HTML tags (<...>)?

    Thank you.

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    Can you give a few examples of what you want replaced? You might also be able to do it with str_replace.

  3. #3
    Join Date
    Jan 2009
    Posts
    10
    a is b <b is not a>
    it should be:
    b is b <b is not a>

    It's very hard to explain. I dont needed any changes in <b is not a> (between <> tags). Only changes in normal text.
    preg_replace changes a to b in tags. This is why I needed preg_replce.
    Last edited by Rytis; 02-05-2009 at 02:05 PM.

  4. #4
    Join Date
    Mar 2007
    Posts
    946
    What values can be inside of the brackets? Is it just html that will be in there?

  5. #5
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by skywalker2208 View Post
    What values can be inside of the brackets? Is it just html that will be in there?
    Yes. Just html. Like
    Code:
    <img src...>
    <a href...>
    etc.

  6. #6
    Join Date
    Mar 2007
    Posts
    946
    I think you could use something like this
    PHP Code:
    /<.+>/ 
    The only problem with that if you had something in your file that had this syntax <something here> then it would pick it up and replace it with whatever you are choosing to replace it with.

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Sometimes and in a controlled environment you can get away with making changes to HTML using regular expressions. But HTML in the wild is way too complicated for that. You'll need a full HTML parser.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    PHP Code:
    $text 'a is b <b is not a>';
    $search 'a';
    $replace 'b';
    $text preg_replace('/'.preg_quote($search).'(?=[^>]*(<|$))/'$replace$text);
    echo 
    htmlentities($text); 
    PS: When you've figured out how that works, you have passed your PCRE exam.
    Last edited by NogDog; 02-05-2009 at 03:18 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jan 2009
    Posts
    10
    Wow. Thank you NogDog

  10. #10
    Join Date
    Nov 2008
    Posts
    157
    Code:
    PCRE exam
    ?
    Sounds sexy

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by mintedjo View Post
    Code:
    PCRE exam
    ?
    Sounds sexy
    About as sexy as a digital prostate exam.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles