PHP regular expression script to remove anything that is not a alphabetical letter or number 0 to 9 and replace space to a hyphen – change to lowercase make sure there is only one hyphen – between words no — or — etc.
For example:
Example: The quick brown fox jumped
Result: the-quick-brown-fox-jumped
Example: The quick brown fox jumped!
Result: the-quick-brown-fox-jumped
Example: The quick brown fox – jumped!
Result: the-quick-brown-fox-jumped
Example: The quick ~`!@#$%^ &*()_+= ——- brown {}|][ :”‘; <>?.,/ fox – jumped!
Result: the-quick-brown-fox-jumped
Example: The quick 1234567890 ~`!@#$%^ &*()_+= ——- brown {}|][ :”‘; <>?.,/ fox – jumped!
Result: the-quick-1234567890-brown-fox-jumped
Anybody have idea for the regular expression?
Thanks!
Since you seem to want all sequences of non-alphanumeric characters being replaced by a single hyphen, you can use this:
But this can result in leading or trailing hyphens that can be removed with
trim:And to convert the result into lowercase, use
strtolower:So all together:
Or in a compact one-liner: