3 に答える
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.
上記の文字のみを置き換えたい場合は、
$cleaned = str_replace(array('®','™','®','™', ":", "'"), '', $string);
通常、通常の文字列置換メソッドはより高速であり、正規表現エンジンのパターン マッチング機能を必要とする置き換えたい例はありません。
コメントによる編集: 文字パターンを置き換える必要がある場合(自分で与えた解決策で示されているように)、正規表現は実際により適切で実用的です。
さらに、McD は、そのスローガンが公開 Web サイトで使用されている場合、両方のシンボルを配置する必要があると確信しています。
® は®
、™ は™
です。そのため、事前に
パターンに従うものはすべて削除する必要があります。&[#0-9a-z]+;
$input = "Remove all ™ and ® symbols, please.";
$string = preg_replace("/&[#0-9a-z]+;/i", "", $input);