php - preg_replace - leaving in unwanted characters -
I have a string:
$ string = "Hello World!";
I want to convert it into a URL-friendly tag, and I have developed a function to do this:
function strip junk ($ string ) {$ String = str_replace ("", "-", $ string); $ String = preg_replace ("/ [^ a-zA-Z] \ s /", "", $ string); $ String = Stroglager ($ string); Return $ string; }
However, when I run my $ string
from above, I get the following:
$ string = "Hello world!";
It seems that my preg_replace is slipped through the characters, even if I understand what I think, they should not be.
It should be read like this:
$ string = "hello-world";
What's going on here? (It should be a smooth smooth lemon scsi!)
Edit 1 : I did not know that regular expression was the initial stuff, but whatever else, \ Removing in the string does not result in desired results.
The desired result is:
- All locations are converted to dashes.
- All remaining characters that are not AZ or 0-9
- The string is then converted to a lower case.
Editing 2 + : My code has been cleared slightly.
The following work is just me:
function string junk ( $ String) {$ string = str_replace ("", "-", trim ($ string)); $ String = preg_replace ("/ [^ a-zA-Z0-9 -] /", "", $ string); $ String = Stroglager ($ string); Return $ string; }
Comments
Post a Comment