i want to get all the a tags with the same class from html file, i have tried:
$html = file_get_contents('http://10tv.nana10.co.il/Category/?CategoryID=400008');
preg_match_all('/<a\s+class="FooterNavigationItemValue">(.*)<\/a>/', $html, $div_array);
return var_dump($div_array);
but i get an empty array, help?
As Marc B commented, using DOM will be your best bet. But since you are looking for regex:
'#<a.*?class="FooterNavigationItemValue".*?>(.*?)</a>#s'
P.S. I looked into the site mentioned in the code and this piece of regex does its job perfectly.
Now the explanation: the two .*?
before and after class="FooterNavigationItemValue"
is to make sure that the string still matches if there's something before and after class="FooterNavigationItemValue"
.
And I used (.*?)
instead of (.*)
to prevent regex greediness. More info can be found here: What do lazy and greedy mean in the context of regular expressions?