I've got a string:
$string = "Hello World!";
I want to turn it into a URL friendly tag, and I've developed a function to do it:
function stripJunk($string){
$string = str_replace(" ", "-", $string);
$string = preg_replace("/[^a-zA-Z]\s/", "", $string);
$string = strtolower($string);
return $string;
}
However, when I run my $string
through it above, I get the following:
$string = "hello-world!";
It seems that there are characters slipping through my preg_replace, even though from what I understand, they shouldn't be.
It should read like this:
$string = "hello-world";
What's going on here? (This should be easy peasy lemon squeasy!)
Edit 1: I wasn't aware that regular expressions were beginners stuff, but whatever. Additionally, removing the \s in my string does not produce the desired result.
The desired result is:
- All spaces are converted to dashes.
- All remaining characters that are not A-Z or 0-9 are removed.
- The string is then converted to lower case.
Edit 2+: Cleaned up my code just a little.