i everyone,, i was thinking how to get a specific value from a outside site and show it on my own. i tried a snippets like this: <?php $source=file_get_contents("http://news.bbc.co.uk/weather/"); $a=preg_match_all('/ <li class="stat"> <div class="statplace"> <a href="(.*?)" title="(.*?)"><strong>(.*?)<\/strong><\/a> <\/div> <div class="statvalue"> <span class="temp temp-34-36"> <span class="cent">([^<]+)<span>/',$source,$b,PREG_SET_ORDER); print_r($b); ?> PHP: i am getting an empty array from preg_match_all(). can anyone help me out please?
There's a CSS class that's variable depending on the temp. Try this. <?php $source = file_get_contents("http://news.bbc.co.uk/weather/"); $pattern = '#<li class="stat">\s*<div class="statplace">\s*<a href="([^"]+)" title="([^"]+)">\s*<strong>(.*)</strong>\s*</a>\s*</div>\s*<div class="statvalue">\s*<span class="temp[^"]*">\s*<span class="cent">([^<]+)<span>#Uis'; $a = preg_match_all($pattern, $source, $b, PREG_SET_ORDER); print_r($b); ?> Code (markup):
thank you it worked but thats not all i wanted. can u explain the preg_match_all code u used. it will be appreciated.
I only made a few changes from your posted code. 1) I replaced all of the literal whitespace with a whitespace "metacharacter", AKA "\s". If I wanted anything that wasn't whitespace I would have used an uppercase S like so "\S". 2) I replaced your "anything zero or more times, ungreedy" captures, AKA "(.*?)" with an "anything other than a closing quote one or more times" capture '([^"]+)' You're almost never going to want an empty match inside an HTML attribute, which is why I use + instead of * and when you know your attribute delimiter is a double quote chances are any double quotes within the attribute will be htmlentities meaning an "anything other than closing quote" works well. I dropped the inline "ungreedy" modifiers in favor of adding the uppercase U flag at the end to mark the entire pattern as being an ungreedy match. 3) I replaced your hard-coded '<span class="temp 12-45"' with something that takes into consideration the variable CSS class names in that element. The first class name appears to always eb "temp", but the second classname has the numeric temp appended to the word temp. Sticking with the "anything other than closing quote" from before, the pattern 'temp[^"]*">' will work around these variable class names.