Page 1 of 1

Image Tag

Posted: Tue Dec 25, 2007 8:46 pm
by it2051229
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>

Posted: Tue Dec 25, 2007 10:09 pm
by Kieran Huggins
What generally are you trying to accomplish?

Posted: Tue Dec 25, 2007 10:40 pm
by John Cartwright

Code: Select all

preg_match_all('!<img.*?src\s*=\s*"([^"]+)!im', ..........);
Untested.

Moved to Regex.

Posted: Wed Dec 26, 2007 5:40 am
by it2051229
oh... preg_match.. i really dont understand preg_match or regex but it's kinda hard to follow.. how bout explaining the code?? and ok, i'll assume that I got the source path, how do i store it in a variable?? and what about if the content has more than one image tag??

Posted: Wed Dec 26, 2007 9:39 am
by John Cartwright
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 mode

Posted: Wed Dec 26, 2007 8:40 pm
by it2051229
Cool.. 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] ??

Posted: Thu Dec 27, 2007 3:42 am
by VladSun
From http://bg2.php.net/function.preg_match_all :
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.e. assume that the whole pattern string is the first array of matches - you should use the second array.

I would modify Jcart's regexp as follows:

Code: Select all

preg_match_all('/<img.*?src\s*=\s*["\'](.+)?["\']/im', $content, $imageSources);
The result would be the value of SRC attribute without surrounding quotes (single or double).

Then you can use:

Code: Select all

foreach($imageSources[1] as $val)
{
	echo $val."<br>"; 
}
or

Code: Select all

for($i=0; $i < count($imageSources[1]); $i++)
{
	echo $imageSources[1][$i]."<br>"; 
}
EDIT: A typo ;)

Posted: Thu Dec 27, 2007 4:40 am
by it2051229
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 ***/
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]

Posted: Thu Dec 27, 2007 8:35 am
by VladSun
Sorry ... should be:

Code: Select all

preg_match_all('/<img.*?src\s*=\s*["\'](.+?)["\']/im', $content, $imageSources);