PHP替换HTML标记以外的字符 [英] PHP replace characters except the HTML tags
问题描述
我需要在字符串中用\xD9\xA0,\xD9\xA1,\xD9\xA2,...,\xD9\xA9
替换字符0,1,2,...,9
.该字符串来自CKEditor,因此它可能包含html标记.使用以下代码
I need to replace the characters 0,1,2,...,9
with \xD9\xA0,\xD9\xA1,\xD9\xA2,...,\xD9\xA9
in a string. This string comes from the CKEditor so it may contains html tags. Using the following code
$body = str_replace("1", "\xD9\xA1", $body);
它将每个1
替换为\xD9\xA1
,因此它会影响标签<h1>
和<table border="1">
,而我只需要替换正文中的数字而不是标签.
it replaces every 1
with \xD9\xA1
so it effects the tag <h1>
and also <table border="1">
while I only need to replace the numbers in the body not the tags.
包含数字的标签是表标签的<h0><h1><h2><h3><h4><h5><h6>
和cellspacing
以及cellpadding
和border
.
The tags that contain numbers are <h0><h1><h2><h3><h4><h5><h6>
and cellspacing
and cellpadding
and border
of table tag.
在不影响<h0><h1><h2><h3><h4><h5><h6>
和cellspacing
和cellpadding
和border
的情况下,如何用上述符号替换数字?
How do I can replace the numbers with the above symbols while it won't effect the <h0><h1><h2><h3><h4><h5><h6>
and cellspacing
and cellpadding
and border
?
推荐答案
您不应使用正则表达式来处理html,但是,如果您仍想使用正则表达式,则可以将丢弃模式与正则表达式一起使用,如下所示:>
You shouldn't use regex to process html, however if you still want to use a regex you could use the discard pattern with a regex like this:
<.*?>(*SKIP)(*FAIL)|1
Working demo
此正则表达式背后的想法是跳过<...>
中的所有内容,但将其余部分匹配.因此,它将仅与不在html标记内的数字1
匹配.再一次,我将使用html解析器代替.
The idea behind this regex is to skip whatever it is within <...>
but match the rest. So, it will only match the number 1
that are not within html tags. Once again, I'd use a html parser instead.
Php代码
$re = "/<.*?>(*SKIP)(*FAIL)|1/";
$str = "<h0><h1><h2><h3>\n<table border=\"1\">\n1\n";
$subst = "\xD9\xA1";
$result = preg_replace($re, $subst, $str);
这篇关于PHP替换HTML标记以外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!