Reputation: 25
I am using regex for replacing everything but a-z A-Z 0-9. I'll like to add that its not removing Slash ( / ) but anyhow it wont work. I dont found the error.
thx for your help!
$string = preg_replace(array('/[^a-zA-Z0-9-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $string);
Upvotes: 1
Views: 6313
Reputation: 8988
You'll need to escape forward slash. I'd try [^-a-z0-9\/]+
to search and replace all unwanted characters.
$re = "/[^-a-z0-9\\/]+/i";
$str = "asdkf\n43435&\$*k((/\\";
$subst = "";
$result = preg_replace($re, $subst, $str);
This will reduce string asdkf\n43435&\$*k((/\\
to asdkf43435k/
See demo https://regex101.com/r/vY2jC7/1
Fix:
I forgot to add i
modifier to ignore case as pointed out by @chris85 which will lead to ignoring upper case letters.
See Updated demo at See demo https://regex101.com/r/vY2jC7/2
Upvotes: 2