I need to to create url from various strings.
So trying to replace all non-alphanumeric characters and all of their combinations - with a hyphen character (-)
$string = "blue - sky";
$string = preg_replace("/[^A-Za-z0-9 ]/", '-', $string);
echo $string;
result - blue---sky
expected - blue-sky.
use the + sign to replace more than one character with one replacement character:
string = preg_replace("/[^A-Za-z0-9]+/", '-', $string);
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