-2

Can any one provide me regex for finding all src tag. I am stuck for last two hours sometimes scripting works some times img but i want regular expression for all.

Thanks in Advance.

mark
  • 21,691
  • 3
  • 49
  • 71
Ajay Kadyan
  • 1,081
  • 2
  • 13
  • 36
  • 1
    [One doesn't simply use regex to parse HTML.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) PHP has a DOM parser so use it. – ThiefMaster Jan 22 '13 at 12:57

2 Answers2

2

Try this:

$pattern = '~<[^>]*?src="([^"]+)"[^>]*>~i';

This should match any tag having src attribute.

Hope this helps.

fuxia
  • 62,923
  • 6
  • 54
  • 62
web-nomad
  • 6,003
  • 3
  • 34
  • 49
0

You can use,

$html = file_get_contents("http://www.example.com");
preg_match_all ( '@src="([^"]+)"@' , $html , $match );
var_dump($match);

EDIT

To be more specific, and get all values for src and SRC in single quote or double quote

preg_match_all ( '@src=([\'"])(.*)([\'"])@' , $html , $match );
preg_match_all ( '@SRC=([\'"])(.*)([\'"])@' , $html , $match2 );
var_dump($match);
var_dump($match2);
user1995997
  • 581
  • 2
  • 8
  • 19
  • Again use preg_match_all function for "SRC" and store it's elements in $match2. So now you have two array $match and $match2. Mix both of them and you have complete list of src. – user1995997 Jan 22 '13 at 13:07