Jump to content

Extracting Data between HTML Tags


Timmins

Recommended Posts

Input String:

<div>Current: <b>Mostly Cloudy</b>

 

I want to extract "Mostly Cloudy" from this input. So far, my code is like this:

$data="<div>Current: <b>Mostly Cloudy</b>";

preg_match_all("<div>Current: <b>((.|\n)*?)</b>",$data,$matches);

print_r($matches);

 

Unfortunately, I continually get an error that the "C" in "Current: " is an unknown modifier. This is really frustrating as i have tried looking for escape characters.. I would greatly appreciate any help!

 

Thanks!

Link to comment
Share on other sites

You have failed to include delimiters so the preg engine is assuming that your delimiters are < >. I'm guessing what you really want is something more like...

 

$data="<div>Current: <b>Mostly Cloudy</b>";
preg_match_all("#<div>Current: <b>(.*?)</b>#s",$data,$matches);
print_r($matches);

Link to comment
Share on other sites

Works perfectly, thanks a million! By any chance could you explain what the hash symbols and the trailing "s" do? Cheers!

 

The hash symbols form the delimiters.. in pcre (which utilises preg functionality), you need to have opening / closing delimiters.

 

From the introduction part of the provided link:

The expression must be enclosed in the delimiters, a forward slash (/), for example. Delimiters can be any non-alphanumeric, non-whitespace ASCII character except the backslash (\) and the null byte.

 

As for the s, this is a modifier (a modifier modifies the behavior of certain things within the pattern).  By default, the dot (.) matches anything excpet a newline.. so if you want to also match newlines with the dot, you add the s modifier after the closing delimiter. You can read up about modifiers here.

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.