Timmins Posted November 12, 2009 Share Posted November 12, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/181325-extracting-data-between-html-tags/ Share on other sites More sharing options...
cags Posted November 12, 2009 Share Posted November 12, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/181325-extracting-data-between-html-tags/#findComment-956511 Share on other sites More sharing options...
Timmins Posted November 12, 2009 Author Share Posted November 12, 2009 Works perfectly, thanks a million! By any chance could you explain what the hash symbols and the trailing "s" do? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/181325-extracting-data-between-html-tags/#findComment-956512 Share on other sites More sharing options...
nrg_alpha Posted November 13, 2009 Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181325-extracting-data-between-html-tags/#findComment-956561 Share on other sites More sharing options...
Timmins Posted November 13, 2009 Author Share Posted November 13, 2009 Brilliant, thanks a million for your help Quote Link to comment https://forums.phpfreaks.com/topic/181325-extracting-data-between-html-tags/#findComment-956888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.