php - Function to generate Friendly URL Strings is not removing Commas -
i have function returns me friendly url string.
public static function geturlfriendlystring($str) { // convert spaces '-', remove characters not alphanumeric // or '-', combine multiple dashes (i.e., '---') 1 dash '-'. $_str = preg_replace("[-]", "-", preg_replace("[^a-z0-9-]", "", strtolower(str_replace(" ", "-", $str)))); return substr($_str, 0, 40); }
anyway, if have example string:
"product vitamins, protein, , lot of stuff"
the resulting string is:
"product-with-vitamins,-protein,-and-a-lot-of-good-stuff"
as can see doesn't remove commas string :/ , knowledge regular expressions null
.
you left out delimiters around regexp, used [
, ]
delimiters. result, weren't being treated character class operators.
if want compress multiple -
one, regexp /-+/
, not [-]
.
public static function geturlfriendlystring($str) { // convert spaces '-', remove characters not alphanumeric // or '-', combine multiple dashes (i.e., '---') 1 dash '-'. $_str = preg_replace("/-+/", "-", preg_replace("/[^a-z0-9-]/", "", strtolower(str_replace(" ", "-", $str)))); return substr($_str, 0, 40); }