[英]How to use preg_match to find html tags?
I'm trying to make this code work by seeing if the returned data contains any html tags with php but it doesn't seem to work.. 我正在尝试通过查看返回的数据是否包含任何带有html的html标记来使此代码正常工作,但是它似乎不起作用。
function returnRegflow()
{
$data = "<html><body>";
return $data;
if(preg_match('html', $data))
{
echo 'error';
} else if(strpos($data, 'regflow') !== false){
echo $data;
}
}
echo returnRegflow();
Some issues: 一些问题:
return
before the if
, so it never got to checking for tags; if
之前通过return
退出为时过早,因此它永远不必检查标签; preg_match
expects the first argument to be regular expression, so it needs a delimiter at the start and the end (like /
); preg_match
期望第一个参数是正则表达式,因此它需要在开始和结束处使用定界符(例如/
); echo
to output the result is not how functions work. echo
输出结果不是函数的工作方式。 Instead return the result, as a string or maybe as a boolean; You seem to be looking for something like this: 您似乎正在寻找这样的东西:
function returnRegflow($data) {
if (preg_match('/<\w/', $data)) {
return 'contains tags';
}
if (strpos($data, 'regflow') !== false){
return $data;
}
return 'does not contain "regflow"';
}
echo returnRegflow("<html><body>");
The check for tags is quite rudimentary: it checks whether the text contains anything that looks like <a
, where a
can be any letter. 对标签的检查非常简单:它检查文本是否包含看起来像
<a
任何内容,其中a
可以是任何字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.