1
4

3 回答 3

8

You're probably having the special characters in entity form, i.e. ® is really ® in your string. So it's not seen by the replacement operation.

To fix this, you could filter for the &SOMETHING; substring, and remove them. There might be built-in methods to do this, perhaps html_entity_decode.

于 2010-01-04T15:22:56.137 回答
4

If you are looking to replace only the mentioned characters, use

$cleaned = str_replace(array('®','™','®','™', ":", "'"), '', $string);

Regular string replacement methods are usually faster and there is nothing in your example you want to replace that would need the pattern matching power of the Regular Expression engine.

EDIT due to comments: If you need to replace character patterns (as indicated by the solution you gave yourself), a Regex is indeed more appropriate and practical.

In addition, I'm sure McD requires both symbols to be in place if that slogan is used on any public website

于 2010-01-04T15:40:55.990 回答
0

® is ®, and ™ is ™. As such, you'll want to remove anything that follows
the pattern &[#0-9a-z]+; before-hand:

$input = "Remove all ™ and ® symbols, please.";
$string = preg_replace("/&[#0-9a-z]+;/i", "", $input);
于 2010-01-04T15:35:24.083 回答