Hi there everyone!

I use a function to strip everything but letters, numbers and spacers for safe part numbers and am having a problem. I thought it would also strip / (and \ ) but it's leaving it.

Here's my function:

function leavealnumspacers( $string ) {
    return preg_replace( "/[^a-z0-9 -_]/i", "", $string );
}

and when I check the output, it's leaving slashes in the string. I thought looking at it that it would only allow - and _. How should I modify the function to disallow all special characters but those two in addition to the letters and numbers?

Thanks for your time!

    My guess is that the problem is that " -" means "space to underscore". ASCII and its derivatives, "space to underscore" includes a whole lot of printable characters, including the digits from 0 to 9 and the entire English alphabet range in uppercase (though that is intended here). One way to fix this is to swap the dash with the underscore, i.e., "/[a-z0-9 -]/i". This way the dash will be interpreted as a character in the character class rather than as an indicator of a range.

      That did indeed work, thanks so much for the help!

        Write a Reply...