开发者

Removing character from php string variable

开发者 https://www.devze.com 2022-12-28 15:55 出处:网络
I have开发者_Python百科 a variable in PHP like Logo™. How do I remove the character ™, if it\'s present?Since it seems like you\'re getting data from an outside source, perhaps it might be better to

I have开发者_Python百科 a variable in PHP like Logo™. How do I remove the character , if it's present?


Since it seems like you're getting data from an outside source, perhaps it might be better to filter out all characters you aren't expecting. What this will include will vary depending on what the variable is for. For example, if you're expecting it to be a single simple word, you could remove anything which isn't a letter.

$str = preg_replace("/[^a-zA-Z]/", "", $str);

Just be careful to think about what is and isn't allowed. The above code would remove accented characters, for example.


$str = str_replace(chr(153), '', 'Logo™');


$str = 'Logo™';
$str = substr_replace('™', '', $str); // Logo
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号