简体   繁体   English

带有html标签的preg_match

[英]preg_match with html tags

I am trying to get a piece of content out of a website that shows the current status from the server. 我试图从一个显示服务器当前状态的网站上获取一段内容。 I did this by the file_get_content method. 我是通过file_get_content方法完成的。 When I try to search through it, i used a preg_match to find the item i want, but it ends up with html tags i cant seem to correctly escape within the code. 当我尝试搜索它时,我使用preg_match来找到我想要的项目,但最终得到的html标签我似乎无法在代码中正确转义。 I get an error W arning: preg_match() [function.preg-match]: Unknown modifier 'B' . 我得到一个错误W arning: preg_match() [function.preg-match]: Unknown modifier 'B' So how do I escape this? 那我怎么逃避这个呢? and further I missed probably a few functions, but I am not really familair with the RegEx. 我进一步错过了一些功能,但我对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";}

Thanks in advance. 提前致谢。

It's a regex. 这是一个正则表达式。 Use a delimiter. 使用分隔符。 Something like this - 像这样的东西 -

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

Better use DOM parsers, but if you really need regex, try to allow spaces: 更好地使用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";}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM