Php preg_与html标记匹配
我试图从显示服务器当前状态的网站中获取一段内容。我是通过Php preg_与html标记匹配,php,html,preg-match,Php,Html,Preg Match,我试图从显示服务器当前状态的网站中获取一段内容。我是通过file\u get\u content方法完成的。当我试图搜索它时,我使用了一个preg_match来查找我想要的项目,但它最终得到了html标记,我似乎无法在代码中正确转义。我收到一个错误Warning:preg_match()[function.preg match]:未知修饰符“B”。那么我该如何逃避呢?此外,我可能错过了一些函数,但我对正则表达式不太熟悉 $content = file_get_contents('http://w
file\u get\u content
方法完成的。当我试图搜索它时,我使用了一个preg_match
来查找我想要的项目,但它最终得到了html标记,我似乎无法在代码中正确转义。我收到一个错误Warning:preg_match()[function.preg match]:未知修饰符“B”
。那么我该如何逃避呢?此外,我可能错过了一些函数,但我对正则表达式不太熟悉
$content = file_get_contents('http://www.swgemu.com/forums/content.php');
$string = '<h3>Basilisk<\/h3><p>Status:<span class=\"online\">Online<\/span>';
if (preg_match($string , $content))
{ echo "The Basilisk server is online"; }
else
{ echo "The Basilisk server is offline";}
$content=file\u get\u contents('http://www.swgemu.com/forums/content.php');
$string='Basilisk状态:在线';
if(preg_匹配($string,$content))
{echo“Basilisk服务器联机”;}
其他的
{echo“Basilisk服务器脱机”;}
提前谢谢。这是一个正则表达式。使用分隔符。像这样的-
$string = '/<h3>Basilisk<\/h3><p>Status:<span class=\"online\">Online<\/span>/';
$string='/Basilisk状态:在线/;
最好使用DOM解析器,但如果确实需要正则表达式,请尝试使用空格:
$content = file_get_contents('http://www.swgemu.com/forums/content.php');
$string = '/<h3>\s*Basilisk\s*<\/h3>\s*<p>\s*Status:\s*<span\s+class=\"online\">\s*Online\s*<\/span>/i';
if (preg_match($string , $content))
{ echo "The Basilisk server is online"; }
else
{ echo "The Basilisk server is offline";}
$content=file\u get\u contents('http://www.swgemu.com/forums/content.php');
$string='/\s*Basilisk\s*\s*\s*状态:\s*\s*在线\s*/i';
if(preg_匹配($string,$content))
{echo“Basilisk服务器联机”;}
其他的
{echo“Basilisk服务器脱机”;}
不要使用正则表达式处理HTML。使用简单的HTML DOM解析器。在$string
中缺少类似#
的分隔符(与@Barmar…)一致。但是,当前的问题是由于缺少分隔符(正则表达式引擎将第一对
视为分隔符)造成的。那么这是否意味着我必须转义所有>而无需转义,只需将其修改为$string='#Basilisk状态:Online#代码>并查看DOM解析器。感谢您的帮助。我还将尝试处理dom解析器。我忘了在状态之后添加空格,但它现在可以工作了。非常感谢。