Image Tag
Moderator: General Moderators
Image Tag
Am currently new here and i'm trying to find a solution on image tags inside an html.
let's just say that i have this image tag (e.g. <img size='50' height='50' src='http://www.whateverwebsite.com/whateverimage.jpg') found inside an html page...
I'm trying to find a way on how am i going to get the source path of the image (e.g. src='http://www.whateverwebsite.com/whateverimage.jpg') which is going to be stored in a variable...
I have been programming with java and used some tools to do this though i can't find it in PHP.. i've been reading the documentations and still haven't found a way to do it... any suggestions you have there or ready made class file that i can make use of???
the source code of my HTML file:
<html>
blah blah blah blah.... <img src='http://www.whateverwebsite.com/whateverimage.com' height='50' width='50'> blah blah blah blah blah and another image <img src='http://www.anotherimage.com/anotherimage.png' style='height:50;width:50'> and blah blah blah.....
</html>
let's just say that i have this image tag (e.g. <img size='50' height='50' src='http://www.whateverwebsite.com/whateverimage.jpg') found inside an html page...
I'm trying to find a way on how am i going to get the source path of the image (e.g. src='http://www.whateverwebsite.com/whateverimage.jpg') which is going to be stored in a variable...
I have been programming with java and used some tools to do this though i can't find it in PHP.. i've been reading the documentations and still haven't found a way to do it... any suggestions you have there or ready made class file that i can make use of???
the source code of my HTML file:
<html>
blah blah blah blah.... <img src='http://www.whateverwebsite.com/whateverimage.com' height='50' width='50'> blah blah blah blah blah and another image <img src='http://www.anotherimage.com/anotherimage.png' style='height:50;width:50'> and blah blah blah.....
</html>
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
preg_match_all('!<img.*?src\s*=\s*"([^"]+)!im', ..........);Moved to Regex.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I can explain the pattern yes, on how to use the function you can check the manual for that
. Also notice I used preg_match_all() and not preg_match()
Code: Select all
!<img.*?src\s*=\s*"([^"]+)!im'
! pattern delimeter
<img match this string literally
.*?src match anything until you find src
\s* match 0 or many spaces (there may or not be a space between)
= match this string literally
\s* match 0 or many spaces
" match this string literally
([^"]+) match all the contents until it runs into the next quote
! pattern delimeter
im case insensitivity and multi line modeCool.. i tried testing the code and somehow it works but still i have some questions left in my mind...
I tried following the manual for the preg_match_all():
/*** start of code ***/
preg_match_all('!<img.*?src\s*=\s*"([^"]+)!im', $content, $imageSources);
foreach($imageSources as $val)
{
echo $val[0]."<br>"; // output: <img style="float: left;" src="../images/13448.jpg (without the ending quoute)
echo $val[1]."<br>"; // output: ../images/13448.jpg <---- This is exactly what i wanted...
}
/*** END of Code ***/
1. how come if i use the old school "FOR LOOP (for $i=0; $i<count($imageSources); $i++)" it doesnt work.. it wont show any output?
2. why does the output of the pregmatch has to be in an ARRAY inside an ARRAY??
3. For the image path, is it always stored at $val[1] ??
I tried following the manual for the preg_match_all():
/*** start of code ***/
preg_match_all('!<img.*?src\s*=\s*"([^"]+)!im', $content, $imageSources);
foreach($imageSources as $val)
{
echo $val[0]."<br>"; // output: <img style="float: left;" src="../images/13448.jpg (without the ending quoute)
echo $val[1]."<br>"; // output: ../images/13448.jpg <---- This is exactly what i wanted...
}
/*** END of Code ***/
1. how come if i use the old school "FOR LOOP (for $i=0; $i<count($imageSources); $i++)" it doesnt work.. it wont show any output?
2. why does the output of the pregmatch has to be in an ARRAY inside an ARRAY??
3. For the image path, is it always stored at $val[1] ??
From http://bg2.php.net/function.preg_match_all :
I would modify Jcart's regexp as follows:
The result would be the value of SRC attribute without surrounding quotes (single or double).
Then you can use:
or
EDIT: A typo 
I.e. assume that the whole pattern string is the first array of matches - you should use the second array.PREG_PATTERN_ORDER
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
I would modify Jcart's regexp as follows:
Code: Select all
preg_match_all('/<img.*?src\s*=\s*["\'](.+)?["\']/im', $content, $imageSources);Then you can use:
Code: Select all
foreach($imageSources[1] as $val)
{
echo $val."<br>";
}Code: Select all
for($i=0; $i < count($imageSources[1]); $i++)
{
echo $imageSources[1][$i]."<br>";
}There are 10 types of people in this world, those who understand binary and those who don't
feyd | Please use
I've learned a new function.. preg_match but i still have to learn the pattern rules cause this preg_match doesnt exist in java....
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Okkk... thank you all of you.. it worked and now am going to continue my project... i mean what would you know, i might end up writing your names as a reference after I finish it...
oh and by the way I've tried also testing the preg_match_all() for double quotes and single quotes annddd
yeah it worked but has still errors in it...Code: Select all
/*** start code ****/
$content = "<img style='float: left;' src='../images/13448.jpg' width='360' height='360' />
<img style=\"height:50; width:50\" src=\"../images/456.gif\">";
preg_match_all('/<img.*?src\s*=\s*["\'](.+)?["\']/im', $content, $imageSources);
foreach($imageSources[1] as $val)
{
echo $val."<br>";
// OUTPUT
// first output : ../images/13448.jpg' width='360' height='360 <--- I noticed it didnt stopped and continued further
// second output: ../images/456.gif <---- it worked for the second image tag maybe because the SRC was found at the last part, but i know it won't work if it has additional image attributes found after the SRC...
}
/*** end of code ***/feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]Sorry ... should be:
Code: Select all
preg_match_all('/<img.*?src\s*=\s*["\'](.+?)["\']/im', $content, $imageSources);There are 10 types of people in this world, those who understand binary and those who don't