Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Remove Alphanumerics Except for +

Last post 11-04-2009, 5:06 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  11-04-2009, 11:06 AM 57135

    Remove Alphanumerics Except for +

    I need some help, guys. This is the first time I've had to work with regex, and it's kicking my butt right now.

    This function is currently taking out all the non-alphanumeric characters from a URL in a URL rewrite and caching script I am using. But I want to change the regex so that the + symbol remains.

    Here's the original function:
    Code:
    function removeNonAlphaNumeric($name, $name_delimiter){
    return preg_replace("/[^a-zA-Z0-9]/", $name_delimiter, $name);
    }



    I've already tried this without success:
    Code:
    function removeNonAlphaNumeric($name, $name_delimiter){
    return preg_replace("/[^a-zA-Z0-9\+]/", $name_delimiter, $name);
    }


    Any ideas?
  •  11-04-2009, 5:06 PM 57148 in reply to 57135

    Re: Remove Alphanumerics Except for +

    In what way is it "without success"? Is the "+" character being taken out (I assume this is what you mean)? Are other characters being taken out that you want left in or left in that you want taken out?

    Perhaps showing us a bit more of the code that calls this function might help, as well as some examples of what you are feeding in and getting/wanting out.

    On the face of it, whatever string is in $name_delimiter should replace every non-alphanumeric except the "+" character in $name just as you want.However the operation of this function is dependent on what $name and $name_delimiter are and you have not told us that.

    The only comment I'd make is that the "+" within the character set definition looses its "normal" meaning as a quantifier and does not need to be escaped.

    Susan

View as RSS news feed in XML