繁体   English   中英

在PHP中删除任何字符,但不删除符号和字母

[英]remove in php any character but not symbols and letters

我如何使用str_ireplace或其他函数删除任何字符,但不删除HTML中常用的字母,数字或符号: " ' ; : . - + = ...等我还想删除/ n,空格,标签和其他。

我需要那个文本,来自做(“textContent”)。 IE10和Chrome中的innerHTML,php变量大小相同,无论哪个浏览器都这样做。因此我需要在两个文本中使用相同的编码,并删除罕见或不同的字符。

我试试这个,但它不适合我:

        $textForMatch=iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
        $textoForMatc = str_replace(array('\s', "\n", "\t", "\r"), '', $textoForMatch);

$ text包含函数的结果(“textContent”)。 innerHTML,我想删除字符为 ó³..

最简单的选择是简单地将preg_replace与白名单一起使用。 即使用列出您要保留的内容的模式,并替换不在该列表中的任何内容:

$input = 'The quick brown 123 fox said "�é³". Man was I surprised';
$stripped = preg_replace('/[^-\w:";:+=\.\']/', '', $input);
$output = 'Thequickbrownfoxsaid"".ManwasIsurprised';

正则表达式的解释

/       - start regex
[^      - Begin inverted character class, match NON-matching characters
-       - litteral character
\w      - Match word characters. Equivalent to A-Za-z0-9_
:";:+=  - litteral characters
\.      - escaped period (because a dot has meaning in a regex)
\'      - escaped quote (because the string is in single quotes)
]       - end character class
/       - end of regex

因此,这将删除正则表达式中列出的任何非单词,数字或特定字符的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM