Jump to content

Remove Special Characters


phpcodec

Recommended Posts

$text = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $text);

 

or i shorter version

$text= preg_replace('/[^\w\d_ -]/si', '', $text);

 

EDIT: the above is a clean up

heres to match

<?php
if (preg_match('/[^\w\d_ -]/si', $text))
{
echo "found unknown characters";
}
?>

Depending on locale, \w *might* match additional characters such as à ò ì ù è. Something to consider. Also, \d can match more than simply 0-9 (example:  0¹²³4). However, I'm not sure if this is locale dependent or not.

 

So depending on what string content is being checked, there may be some pitfalls if one is not careful.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.