SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression to ignore a character if another is found before it.

    Lets say we have these two strings...

    Ok &#;

    and...

    Okdsd#k

    How can I do a preg_replace on these strings and only do a replace on the '#' character if the previous character is -NOT- &. So for instance the regular expression would only do the replace in the second string.

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $regex "~(?<!&)#~";

    $text1 "Ok &#";
    $repl1 preg_replace($regex"xxx"$text1);

    $text2 "Okdsd#k";
    $repl2 preg_replace($regex"xxx"$text2);

    echo 
    "$text1<br />$repl1<br />$text2<br />$repl2"
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


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
  •