Regex: IMG TAG
As i said before, i wanna share my Regex code, to catch the file name in HTML IMG TAG. Let me explain why i create this Regex code. I have IMG TAG that i need to catch only the file name. I don't care about other attribute (ex. border="0") all i want just the file name inside the src attribute (ex. src="file/path/i_want_this.jpg");
First i have this code
-
<img src="file/path/to/file_name.jpg" border="0" alt="Test" title="Test" hspace="7" width="100" height="68" align="left" />
And i want to capture the file_name.jpg, i use this
-
<img src\s*="file/path/to/\s*(?:\'|")?(?P<srcvalue>[^\'">]*?)(?:\'|"|\\s) ([^>]*)>
After i get the file_name.jpg, i want to use it on hyperlink and also i want to change the path to absolute. The result should be like this
See, in HTML IMG TAG i just change the src attribute, i leave other attribute value like before. So i made some function in PHP using preg_replace to change that with Regex. Heres the code
-
function get_image($content){
-
$content = preg_replace('/<img src\\s*="file\/path\/to\/\\s*(?:\'|")?(?P<srcvalue>[^\'">]*?)(?:\'|"|\\s) ([^>]*)>/', '<a href="absolute_path/$1" target="_blank"><img src="absolute_path/$1" $2></a>', $content);
-
return $content;
-
}
Thats it. You can modify the Regex to fulfill your need. FYI to get all the src value in HTML IMG TAG just change the Regex like this
-
<img src\s*="\s*(?:\'|")?(?P<srcvalue>[^\'">]*?)(?:\'|"|\\s) ([^>]*)>
Happy Coding ![]()

ups, fix some typos:
pattern =
PLAIN TEXTCODE:
'/<img.*?src=([\'"])([^\1]+)\1/i'
example:
PLAIN TEXTPHP:
$text = '<img width=200 src="file/path/i_want_this.jpg" height=100>';
preg_match('/<img.*?src=([\'"])([^\1]+)\1/i', $text, $match);
echo "src: $match[2]\\nbasename: ", basename($match[2]));
Hello! Good Site! Thanks you! drbputxviesgo
Is it possible to extract more then one image from a string containing many images?
I have yet to find a script that can work with all img tag syntax's!
Thanks
Try it first mate....
The src-submatch was greedy, so if there is more than one -tag use this instead:
<img.+?src=([\'"])([^\1]+?)\1
Thanx for your suggestion.