I am getting strings from a database and then using the strings to build a URL. My issue is, some of the strings will have characters like < > & { } * general special characters, but the string may also have strings in. How would I replace the spaces with dashes and totally remove and special characters from the strings?
Keep only alphabets and numbers in a string using preg_replace
:
$string = preg_replace('/[^a-zA-Z0-9-]/', '', $string);
You can use str_replace
to replace space with -
$string = str_replace (" ", "-", $string);
Look at the following article:
With str_replace
:
$str = str_replace(array(' ', '<', '>', '&', '{', '}', '*'), array('-'), $str);
Note:
If replace has fewer values than search, then an empty string is used for the rest of replacement values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With