Jump to content

Preg_match not finding a string in HTML


livewire1974

Recommended Posts

I am trying to extract a table from HTML, here is the HTML code for the start of the table.

 

 

<table class='price' id='comp' style='clear:both;display:none'>

But when I use this PHP code, no matches are found

 

 

preg_match("/<table class='price' id='comp' style='clear:both;display:none'>.*?<\/[\s]*table>/s", $buffer, $matches);
print_r($matches);

 

I guess there is something wrong with the preg_match statement?

 

 

Link to comment
https://forums.phpfreaks.com/topic/256758-preg_match-not-finding-a-string-in-html/
Share on other sites

if you want to match that exact format, you can use this.

 

$str = "<table class='price' id='comp' style='clear:both;display:none'>";
$pattern = '~<table class=("|\')price("|\') id=("|\')comp("|\') style=("|\')clear:both;display:none("|\')>~';
preg_match($pattern,$str,$matches);
print_r($matches);

 

where $matches[0] will contain the matched string.

 

if you want this regex to match a dynamic beginning <table> element, I will have to revise it.

oh okay, of course it's possible:

 

$str = "<table class='price' id='comp' style='clear:both;display:none'>this is a test</table>";
$pattern = '~<table[^>]*>([^<]*)</table>~';
preg_match($pattern,$str,$matches);
print_r($matches);

 

$matches[0] will contain the entire matched string and $matches[1] will contain the text between <table</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.