php 检测字符串中的 HTML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5732758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect HTML tags in a string
提问by bcmcfc
I need to detect whether a string contains HTML tags.
我需要检测一个字符串是否包含 HTML 标签。
if(!preg_match('(?<=<)\w+(?=[^<]*?>)', $string)){
return $string;
}
The above regex gives me an error:
上面的正则表达式给了我一个错误:
preg_match() [function.preg-match]: Unknown modifier '\'
I'm not well up on regex so not sure what the problem was. I tried escaping the \ and it didn't do anything.
我对正则表达式不太了解,所以不确定问题是什么。我试图逃避 \ ,但没有做任何事情。
Is there a better solution than regex? If not, what would be the correct regex to work with the preg_match?
有比正则表达式更好的解决方案吗?如果不是,那么与 preg_match 一起使用的正确正则表达式是什么?
回答by Diarmaid
A simple solution is:
一个简单的解决方案是:
if($string != strip_tags($string)) {
// contains HTML
}
The benefit of this over a regex is it's easier to understand, however I could not comment on the speed of execution of either solution.
与正则表达式相比,这样做的好处是更容易理解,但是我无法评论任一解决方案的执行速度。
回答by simon
you need to 'delimit' the regex with some character or another. Try this:
您需要使用某个字符或其他字符“分隔”正则表达式。尝试这个:
if(!preg_match('#(?<=<)\w+(?=[^<]*?>)#', $string)){
return $string;
}
回答by Gerfried
If you just want to detect/replace certain tags: This function will search for certain html tags and encapsulate them in brackets - which is pretty senseless - just modify it to whatever you want to do with the tags.
如果您只想检测/替换某些标签:此功能将搜索某些 html 标签并将它们封装在括号中 - 这是非常没有意义的 - 只需将其修改为您想要对标签执行的任何操作。
$html = preg_replace_callback(
'|\</?([a-zA-Z]+[1-6]?)(\s[^>]*)?(\s?/)?\>|',
function ($found) {
if(isset($found[1]) && in_array(
$found[1],
array('div','p','span','b','a','strong','center','br','h1','h2','h3','h4','h5','h6','hr'))
) {
return '[' . $found[0] . ']';
};
},
$html
);
Explaination of the regex:
正则表达式的解释:
\< ... \> //start and ends with tag brackets
\</? //can start with a slash for closing tags
([a-zA-Z]+[1-6]?) //the tag itself (for example "h1")
(\s[^>]*)? //anything such as class=... style=... etc.
(\s?/)? //allow self-closing tags such as <br />
回答by slsdoug
I would use strlen()
because if you don't, then a character-by-character comparison is done and that can be slow, though I would expect the comparison to quit as soon as it found a difference.
我会使用,strlen()
因为如果你不这样做,那么逐个字符的比较就会完成,这可能会很慢,但我希望比较在发现差异后立即退出。
回答by Reza Saadati
I would recommend you to allow defined tagsonly! You don't want the user to type the <script>
tag, which could cause a XSS vulnerability.
我建议您只允许定义的标签!您不希望用户键入<script>
标签,这可能会导致 XSS 漏洞。
Try it with:
试试看:
$string = '<strong>hello</strong>';
$pattern = "/<(p|span|b|strong|i|u) ?.*>(.*)<\/(p|span|b|strong|i|u)>/"; // Allowed tags are: <p>, <span>, <b>, <strong>, <i> and <u>
preg_match($pattern, $string, $matches);
if (!empty($matches)) {
echo 'Good, you have used a HTML tag.';
}
else {
echo 'You didn\'t use a HTML tag or it is not allowed.';
}
回答by MutantMahesh
If purpose is just to check if string contain html tag or not. No matter html tags are valid or not. Then you can try this.
如果目的只是检查字符串是否包含 html 标签。无论 html 标签是否有效。那你可以试试这个。
function is_html($string) {
// Check if string contains any html tags.
return preg_match('/<\s?[^\>]*\/?\s?>/i', $string);
}
This works for all valid or invalid html tags. You can check confirm here https://regex101.com/r/2g7Fx4/3
这适用于所有有效或无效的 html 标签。您可以在这里检查确认https://regex101.com/r/2g7Fx4/3
回答by Addys
Parsing HTML in general is a hard problem, there is some good material here:
通常解析 HTML 是一个难题,这里有一些很好的材料:
But regarding your question ('better' solution) - can be more specific regarding what you are trying to achieve, and what tools are available to you?
但是关于您的问题(“更好”的解决方案) - 可以更具体地说明您要实现的目标以及您可以使用哪些工具?
回答by clamchoda
If your not good at regular expressions (like me) I find lots of regex libraries out there that usually help me accomplish my task.
如果你不擅长正则表达式(像我一样),我会发现很多正则表达式库通常可以帮助我完成任务。
Here is a little tutorial that will explain what your trying to do in php.
这是一个小教程,将解释您在 php 中尝试做什么。
Here is one of those librariesI was referring to.
这是我所指的那些库之一。