Preg Match All Problem!

Hi all, I’m pulling my hair out, been trying to work out how to parse some html between a span tag. My code is below, any ideas what I’m doing wrong would be appreciated :slight_smile:

preg_match_all('|<span style="font-family: Arial;font-size: 18px;">(.*)</span>|',$html,$matches);

what exactly isn’t working?

since we don’t know what the exact errorr is I would sugest to add abckslashes

preg_match_all('|<span style=\\"font-family: Arial;font-size: 18px;\\">(.*)<\\/span>|',$html,$matches);  

Hi there, I’m trying to get the product name between the span tag as below. But it doesn’t seem to be wanting to parse it. I’m using print_r($matches) to see if I’m getting any results but I’m getting nothing.

<span style="font-family: Arial;font-size: 18px;">Example Product Hello World</span>
$html = file_get_contents('http://www.example.com');

preg_match_all('|<span style=\\"font-family: Arial;font-size: 18px;\\">(.*)<\\/span>|',$html,$matches); 

print_r($matches);

try

echo $matches[0][1];

matches returns an array of array of matches… … best way to see this is to do a var_dump($matches) you can then see exactly where in the array your desired value resides.

Hey bud, I’ve just tried echoing and using var_dump and the array is empty, nothing in there. Just can’t fathom out why the thing isn’t working!:confused:

hmmm, are you sure you are getting the right $html back? it has happened to me before that the problem was been in the data and not the regex.

without knowing what url you are trying to scrape I can only guess, but most likely you are missing some extra whitespace or something. try something more generic like:


preg_match_all('|<span\\s+style="font-family:\\s*Arial;\\s*font-size:\\s*18px;">(.*?)</span>|',$html,$matches);

Also the backslashes aren’t needed

aamonkey might have a point, as I said check your $html value, by outputting it and looking at the SOURCE CODE…

but also try this :
preg_match_all(‘|<span style=\“font-family: Arial;font-size: 18px;\”>(.*)<\/span>|’, '<span style=“font-family: Arial;font-size: 18px;”>Example Product Hello World</span>, $html, PREG_PATTERN_ORDER);