简体   繁体   中英

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. 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. I get an error 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.

$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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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