How can I exclude @ # $%^&* from a given string?
Try this:
$str= preg_replace("/[^a-zA-Z0-9_\-\/=]+/", "", 'your string here');
This allows only common acceptable chars. Excludes the chars you mentioned.
Or you can try this too:
$str = '@ # $%^&*';
$new_str = str_replace(array('@', '#', '$', '%', '^', '&', '*'), '', $str);
print $new_str;
You can specify multiple characters to replace using str_replace
:
$s= 'what a @bad #$%^ string *';
$s= str_replace(array('@', '#', '$', '%', '^', '%', '*'), '', $s);
echo($s);
This will ouput:
what a bad string
$thisIsaVeryBadStringIndeed = "@wh#at %a b^a&d @#$%^&*string";
$unWantedBadCharacters = "@#$%^&*";
$chars = preg_split('//',$unWantedBadCharacters);
for ($i=0;$i<strlen($unWantedBadCharacters);++$i)
$pairs[$unWantedBadCharacters{$i}] = '';
$stringWithoutBadCharacters = strtr($thisIsaVeryBadStringIndeed,$pairs);
This is one of the faster methods. If you only create the pairs array once.
A simple regular expression would be one way to do it:
$str = preg_replace('/[@#$%^&*]/', '', $str);