Home  >  Article  >  Backend Development  >  去除php字符串中的中文特殊字符

去除php字符串中的中文特殊字符

WBOY
WBOYOriginal
2016-06-06 20:49:251572browse

现在有一个类似这样的中文字符【你好!】

我现在想将中文的字符【】!去掉(当然不只是这3种)

应该怎么做呢?

回复内容:

现在有一个类似这样的中文字符【你好!】

我现在想将中文的字符【】!去掉(当然不只是这3种)

应该怎么做呢?

只保留中文的话

<code class="lang-php"><?php //确保字符串为utf-8
echo preg_replace('~[^\p{Han}]~u', '', '【你好!】'),"\n";
</code></code>

比较简单的就是用preg_replace做了,虽然你指出不止那几个字符,但是中文符号也没几个吧,不防可以都打上的说,去除也可以,或者替换成因为的也成。

<code>echo preg_replace('/[【!】]/', '', '【你好!】');
</code>

OR

<code>$preg = array('【','】','!');
$replace = array('[', ']', '!');

function add_slash($item)   return '/'.$item.'/';
$preg = array_map('add_slash', $preg);

echo preg_replace($preg, $replace, '【你好!】');
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn