带有html标签的preg_match

时间:2014-02-05 19:15:29

标签: php html preg-match

我正试图从一个显示服务器当前状态的网站上获取一段内容。我是通过file_get_content方法完成的。当我尝试搜索它时,我使用preg_match来找到我想要的项目,但最终得到的html标签似乎无法在代码中正确转义。我收到错误W arning: preg_match() [function.preg-match]: Unknown modifier 'B'。那我怎么逃避这个呢?我进一步错过了一些功能,但我并不熟悉RegEx。

$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";}

提前致谢。

2 个答案:

答案 0 :(得分:1)

这是一个正则表达式。使用分隔符。像这样的东西 -

$string = '/<h3>Basilisk<\/h3><p>Status:<span class=\"online\">Online<\/span>/';

答案 1 :(得分:1)

更好地使用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";}
相关问题