Copy link to clipboard
Copied
I started out wanting to replace spaces, special characters etc. with dashes. However, I can't seem to find the right way to replace a forward slash with a dash. I've looked all over the internet, some people suggesting using the .htaccess-file though I don't think that fits my purpose. My coding so far:
Example: You've got 1 new message (12/07/2016)
As I would like to have it: you-ve-got-1-new-message-12-07-2016
<?php
$message_original = $row_message ['title'];
$message_lower = strtolower($message_original);
$message_alpha = preg_replace("/[^a-z0-9_\s-]/", "", $message_lower);
$message_dash = preg_replace("/[\s-]+/", " ", $message_alpha);
$message = preg_replace("/[\s_]/", "-", $message_dash);
?>
1 Correct answer
$string = "You've got 1 new message \$! (12/07-2016)";
$filtered = strtolower(preg_replace('/[\W\s\/]+/', '-', $string));
echo $filtered; // you-ve-got-1-new-message-12-07-2016
You can do it with the simplified code above. The regular expression '/[\W\s\/]+/' is a pattern group that contains three metacharacters:
- \W matches any character that isn't alphanumeric or the underscore (it's the same as [^A-Za-z0-9_]).
- \s matches any whitespace character.
- \/ matches a forward slash.
Copy link to clipboard
Copied
$string = "You've got 1 new message \$! (12/07-2016)";
$filtered = strtolower(preg_replace('/[\W\s\/]+/', '-', $string));
echo $filtered; // you-ve-got-1-new-message-12-07-2016
You can do it with the simplified code above. The regular expression '/[\W\s\/]+/' is a pattern group that contains three metacharacters:
- \W matches any character that isn't alphanumeric or the underscore (it's the same as [^A-Za-z0-9_]).
- \s matches any whitespace character.
- \/ matches a forward slash.

