I want to remove all characters except Alphabets, Numbers and Dashes. Here is my code
$tracking = "(TCS-123412&2)";
$tracking = preg_replace("/[^ \w]+/", "", $tracking);
echo $tracking;
The output is = TCS1234122
I want the output should be = TCS-1234122
Please help.
Regards
You need to include all characters which you do not want to exclude in the negative character class. Note that \w
also includes underscore, which you presumably want to retain.
$tracking = "(TCS-123412&2)";
$tracking = preg_replace("/[^A-Za-z0-9-]+/", "", $tracking);
echo $tracking; // TCS-1234122