tags:

views:

57

answers:

2

What's the best way to remove symbols, like the Registered, Trademark, and Copyright symbols from a string?

For instance I'd like to strip the ® out of the following: $string = 'Can't Touch This®';

+2  A: 
echo ord('®'); // Returns 194

Figure out the values for other chars you don't want and then... (Note that this mark is actually represented by two 'characters'. So you may want to loop over the entire string instead)

$string = 'Can\'t Touch This®';
$newstring = '';
$forbidden = array(174,194);
for($i=0;$i<strlen($string);$i++){
    if(in_array(ord($string[$i]),$forbidden)) continue;
    else $newstring.= $string[$i];
}
echo $newstring;

[Edit]

Myeah, as I suggested you might want to loop over the entire string:

$string = 'Can\'t Touch This™';
$newstring = '';
$forbidden = array(174,194);
for($i=0;$i<strlen($string);$i++){
    if(in_array(ord($string[$i]),$forbidden)) continue;
    else $newstring.= $string[$i];
    echo ord($string[$i]).",";
}

Returns [...]226,132,162, Aha! THREE numbers!

$forbidden = array(174,194,132,162,226);

Works

Robus
It works great for the registered symbol, but when I tried Can't Touch This™(226) it returned this: Can't Touch This¢
hsatterwhite
Updated after your comment
Robus
Robus you rock! Thanks for the help :)
hsatterwhite
A: 

If you know what you want to remove, then use str_replace()

For instance:

 $my_string = str_replace(array('®', '™'), array('', ''), $my_string)

If you've got a long list of 'forbidden' characters, you could use array_fill(0, count($forbidden_list), '')

staticsan