Forum Moderators: coopster
<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>
array1 (
[0]=>img1.gif
[1]=>img3.gif
)
array2 (
[0]=>img2.gif
)
<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>
<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>
<?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);
}
?>
<?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);
?> <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]