|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
Welcome to DevShed Forums, mattmauldin.
![]() Try this: Code:
function removeNonAlphaNumeric($name, $name_delimiter){
return preg_replace("/[^a-zA-Z0-9\+\040]/", $name_delimiter, $name);
}
The "\040" means a space character.
__________________
Spreading knowledge, one newbie at a time. Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. Check out my blog. |
|
#3
|
||||
|
||||
|
I'd recommend adding a + (as in repetition):
Code:
/[^...]+/ Without, "Programming Languages - More" could turn into "Programming-Languages---More". With, you get "Programing-Languages-More". I'd also recommend trimming off the $name_delimiter from the start and end. Code:
"Programming Languages (More)" before -> "Programming-Languages-More-" after regex -> "Programming-Languages-More" after trim |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Regex Programming > Remove Alphanumerics Except for + |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|