我如何使用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,我想删除字符为 é³..
答案 0 :(得分:3)
最简单的选择是简单地将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
因此,这将删除正则表达式中列出的任何非单词,数字或特定字符的内容。