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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    preg_replace() Unknown modifier

    Hello everyone

    I have a problem with preg_replace(). It's probably something stupid that I simply fail to see, but I can't figure out what's wrong.

    The thing is, I have a string that has a lot of information I don't need, and I use preg_replace to strip that out.

    For this, I use the following code:
    PHP Code:
        $regex "'?;? ?option\['\d+'\] = '";
        
    $replace ", ";
        
        echo 
    preg_replace($regex$replace$string); 

    I tested this using http://gskinner.com/RegExr/ to be sure the regular expression isn't wrong. On that page it works exactly the way I want it to work, but when I use it in my PHP code it tells me
    Warning: preg_replace() [function.preg-replace]: Unknown modifier '\' in **filename** on line 13

    Can anyone tell me what I'm doing wrong, and more importantly how to do it right?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PCRE regex must be delimited in order to use.

    Edit:
    For further explanation as to what the problem is, it considers your ' as a delimiter. Therefore, all characters found after the delimiter need to be modifiers, which \ is not.

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Ah, I understand. Thank you for that link.

    However, now it seems like I have a new problem. I changed the code to $regex = "{'?;? ?option\['\d+'\] = '}"; to include the delimiter, but now it seems I have another problem: the '?;? ? part of the expression seems to be ignored completely.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What are you trying to match against?

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    The string I recieve is basically
    option['34'] = 'sometext'; option['3'] = 'someothertext'; option['9000'] = 'moretext'; option['42'] = 'texttexttext';
    and so on. For this function I just want to have
    sometext, someothertext, moretext, texttexttext
    I get the original string from a different function I'm not allowed to touch at all (it's not mine), so I figure I'd just strip out the parts I don't need with preg_replace().

    I realize this is probably not the best way to do it since it leaves some junk at the beginning and the end of the string, but as long as it's only the beginning and the end I can just substr() it away and still end up with exactly what I need.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Works for me. You'll need to ltrim off the ', ' from the string though since it replaces it otherwise.
    I used this: $pattern = "{'?;?\s*option\['\d+'\] = '}";

  • Users who have thanked Fou-Lu for this post:

    Aargh (11-23-2011)

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Now it works, thanks for your help. I guess the problem was ? vs \s*.


  •  

    Tags for this Thread

    Posting Permissions

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