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
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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.