Forum Moderators: coopster
$text = 'this is a ... "TEST" ?!';
$text = preg_replace("/\s+/", '-',
preg_replace("/&[\w#]{2,5};|^[^a-z0-9]+|[^a-z0-9\s]|[^a-z0-9]+$/", '',
strtolower($text))
);
// same as above, but with comments to explain each step
$text =
// finally, the only thing left should be letters, numbers, and \s
// convert \s to -
preg_replace("/\s+/", '-',
// line breaks added for readability
preg_replace("/
&[\w#]{2,5}; | // remove &...;
^[^a-z0-9]+ | // remove opening characters that aren't [a-z0-9]
[^a-z0-9\s] | // remove anything that's not a letter, number, or \s
[^a-z0-9]+$ // remove trailing characters that aren't [a-z0-9]
/", '',
// lowercase first, so no need to use /i above
strtolower($text))
); csdude55: My goal here is to ONLY have letters and numbers, with words delimited with a -. This is used for make search-engine-friendly links that look like:Did you solve this? I'm surprised nobody has replied, this is a bit old.
example.com/this-is-a-test/123
Do you see any way to improve it?
I'm surprised nobody has replied, this is a bit old.Sometimes posts mysteriously become invisible--and then, days or weeks or months later, just as mysteriously reappear. I don't think I remember this one either; the preg replace() would have jumped out at me beccause I only speak about three words of php, and that’s one of them.
$filename = strtolower(preg_replace("/\W/","",$inner[0]))