I am trying to prepare a string for use as a directory listing. I am working with user entered titles and need to remove all but letters and numbers, then replace spaces with _.. I have been able to get somewhat close to this with preg_replace but don’t have hours to learn the ins and outs of Regex at the moment.
Here is an example of the string = This is a -string- that has /characters & other stuff! Let's filter it. What I am shooting for is:
this_is_a_string_that_has_characters_other_stuff_lets_filter_it
I was able to get close with this code, but places where a character was removed the space after it remained. The result was double _’s, which is acceptable but not what I was shooting for.
Any help would be greatly appreciated. Here’s my attempt:
<?php
$string = " This is a -string- that has /characters & other stuff! Let's filter it?";
$cleansedstring = trim($string);
$cleansedstring = strtolower($cleansedstring);
$cleansedstring = preg_replace('/[^ \w]+/', '', $cleansedstring);
$cleansedstring = preg_replace('[\s]', '_', $cleansedstring);
echo $cleansedstring;
?>
UPDATED
Here is what I have figured out from some suggestions on here, seems pretty clean and outputs the string I was shooting for.. suggestions on improvement?
$string = " This is a -string- _that has /characters & other stuff! Let's filter it?23";
$cleansedstring = trim($string);
$cleansedstring = strtolower($cleansedstring);
$cleansedstring = preg_replace('/[^ \pL \pN]/', '', $cleansedstring);
$cleansedstring = preg_replace('[\s+]', '_', $cleansedstring);
echo $cleansedstring;
The regex for removing unwanted characters should not have a
+there, and the one that checks for spaces needs a+after it.