PHP RegExp Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a2rodger
    New Member
    • Dec 2007
    • 9

    PHP RegExp Help!

    I have the following regular expression which is stripping all unwanted characters before sending a variable to a url.

    $url = preg_replace('~[^\\pL0-9_]+~u', '_', $url);

    The problem is I would like to keep single quotation marks or encode them for the url so that I may retrieve them on the next page. The function above is currently getting rid of all single quotes.

    I hacked this together and am not sure how to do this. Please Help!

    Aaron
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Just add a single quote (preceeded with a backslash) to the regex, then urlencode() the url.
    [php]
    $_url = "something'with ' sing l e ' quotes in";
    echo urlencode(preg_ replace('~[^\\pL0-9_\']+~u', '_', $_url));
    [/php]

    Comment

    • a2rodger
      New Member
      • Dec 2007
      • 9

      #3
      That did the trick. Thanks!

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by a2rodger
        That did the trick. Thanks!
        No probs!
        remember to urldecode() when you want to get the url params!

        Comment

        Working...