homepage Welcome to WebmasterWorld Guest from 54.145.209.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
how to check if image is linked
how to check if image is linked
nasser bahaj



 
Msg#: 4122722 posted 10:24 am on Apr 27, 2010 (gmt 0)

hi all

is there any way to check images on my page are linked or not ?

example :
my page html code :

<html><body>
<img src="img1.gif" /><br />
<a href="www.site1.com"><img src="img2.gif" /></a><br />
<img src="img3.gif" /><br />
</body></html>


in the page above I have 3 images one of them linked and the others not linked.

so I want a way to get array of linked images and another array of not linked images .


array1 (
[0]=>img1.gif
[1]=>img3.gif
)

array2 (
[0]=>img2.gif
)


I think this can done by regular expression but I could not do it .

best regards .

 

lorum



 
Msg#: 4122722 posted 11:35 am on Apr 27, 2010 (gmt 0)

What is your intent exactly?

When do you want to create these arrays, and what for?

nasser bahaj



 
Msg#: 4122722 posted 12:40 pm on Apr 27, 2010 (gmt 0)

thanks mr lorum for your reply

see

I have this html code


<html><body>
<img src="img1.gif" /><br />
<a href="www.site1.com"><img src="img2.gif" /></a><br />
<img src="img3.gif" /><br />
</body></html>


I want to parse it and get the not linked images (not inside <a> element) then link them to my home page

so I want the above code become like this.


<html><body>
<a href="www.mysite.com"><img src="img1.gif" /><a><br />
<a href="www.site1.com"><img src="img2.gif" /></a><br />
<a href="www.mysite.com"><img src="img3.gif" /><a><br />
</body></html>


mysite.com = my site
site1.com = another site

take look at the code

I want to add link to image has not link already (img1.gif and img3.gif)

thanks

lorum



 
Msg#: 4122722 posted 1:09 pm on Apr 27, 2010 (gmt 0)

Perhaps you could use a multidimensional array, like this:


<?php
$images = array( array("", "img1.gif"),
array("www.site1.com", "img2.gif"),
array("", "img3.gif")
);

for ($row = 0; $row < count($images)-1; $row++)
{
$imgstr;
if(strlen($images[$row][0]) == 0)
{
$imgstr .= "<a href='www.mysite.com'>";
}
else
{
$imgstr .= "<a href='".$images[$row][0]."'>";
}
$imgstr .= "<img src='".$images[$row][1]."'></a>";
print($imgstr);
}
?>


The array has an URL and an image name. If the URL is 0 characters long, it inserts a default link, else it inserts the link that is set in the array.


Edit: Sorry, I didn't understand what you wanted, so this code will probably not help you.

lorum



 
Msg#: 4122722 posted 1:48 pm on Apr 27, 2010 (gmt 0)

I have created a bit of code that hopefully does what you want (albeit very messy and not clean). You can modify it to your own needs.

<?php

// The HTML code (can also load it with file())
$html = '<html><body>
<img src="img1.gif" /><br />
<a href="www.site1.com"><img src="img2.gif" /></a><br />
<img src="img3.gif" /><br />
</body></html>';

//Explode HTML code per element
$explhtml = explode("<",$html);

$parsedhtml = '<html><body>';

//Loop through all the elements in the HTML code
for ($row = 0; $row < count($explhtml)-1; $row++)
{
//If the current element is 'img', check if the previous element is 'a'. If not true, add a default <a> element. Else put in the original element.
if (preg_match("/^img/", $explhtml[$row], $matches)) {

if (!preg_match("/^a/", $explhtml[$row-1], $matches)) {
$parsedhtml .= '<a href="http://www.my-site.com/">';
$parsedhtml .= '<' . $explhtml[$row];
$parsedhtml .= '</a>';
}
else{
$parsedhtml .= '<' . $explhtml[$row-1];
$parsedhtml .= '<' . $explhtml[$row];
$parsedhtml .= '</a>';
}
$parsedhtml .= '<br />';
}

}
$parsedhtml .= '</body></html>';

print($parsedhtml);

?>


Output of the code:

<html><body><a href="http://www.my-site.com/"><img src="img1.gif" /></a><a href="www.site1.com"><img src="img2.gif" /></a><a href="http://www.my-site.com/"><img src="img3.gif" /></a></body></html>

[edited by: lorum at 2:16 pm (utc) on Apr 27, 2010]

nasser bahaj



 
Msg#: 4122722 posted 2:12 pm on Apr 27, 2010 (gmt 0)

very very great Mr lorum

this is what I meaning exactly.

I informed about your reply while I trying to explain your previous reply.

but this is work very will

thank you very much Mr lorum.

best regards

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved