Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Regex Pattern Question

    i have been wanting to figure this out for a while now,

    say you have:

    PHP Code:
    $text "<b>test</b><b>test</b>";
    $text preg_replace("/\<b\>(.*)\<\/b\>/","[b]$1[/b]",$text); 
    the "$text" variable is going to hold:
    PHP Code:
    [b]test</b><b>test[/b
    but what i WANT it to hold, is this:
    PHP Code:
    [b]test[/b][b]test[/b

    one thing i have done in the past is this:
    PHP Code:
    "/\<b\>([a-zA-Z0-9]+)\<\/b\>/" 
    but i dont want to have to specify all and every character that can be pressed on a keyboard.

    Can anyone help me, please?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Add the modifiers U, s, and i.

    'U' makes the pattern ungreedy (and will fix your initial problem)
    's' enables multi-line matching (new lines between <b> and </b>)
    'i' makes it case insensitive (match both <b></b> and <B></B>)

    PHP Code:
    $text preg_replace("/\<b\>(.*)\<\/b\>/Usi","[b]$1[/b]",$text); 


  •  

    Posting Permissions

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