Forum Moderators: coopster
I want to find each image and apply a class to it.
achieved that.
preg_replace(
'/\s(href¦src)=["\']?\/?(?!(https?:))([^>"\'\s]+)/i',
' $1="../$3" class="imgtank',
$row['content']
);
Now i want to wrap a href tag around it so i can use lightbox.
previous output is:
<img src="../beheer/uploads/akf_naarden_056.jpg" class="imgtank" />
has to become:
<a href="../beheer/uploads/akf_naarden_056.jpg" rel="lightbox[page]">
<img src="../beheer/uploads/akf_naarden_056.jpg" class="imgtank" />
</a>
These regular expressions are twisting my mind, tried all kind of stuff but didn't came up with a working one.
Any suggestions are appreciated
[edited by: coopster at 3:55 pm (utc) on June 21, 2008]
[edit reason] Disable graphic smile faces [/edit]
$string="<img src=\"../beheer/uploads/akf_naarden_056.jpg\" class=\"imgtank\" />";$pattern="/<img src=\"([^\"]+)\" class=\"imgtank\" \/>/";
$replace="<a href=\"$1\" rel=\"lightbox[page]\"><img src=\"$1\" class=\"imgtank\" /></a>";
$string=preg_replace($pattern,$replace,$string);
And welcome to WebmasterWorld, ritz :)
[edited by: Receptional_Andy at 4:09 pm (utc) on June 21, 2008]
This is the code i have so far.
I think it can be simplefied, but for now it's working.
<?php
$text = preg_replace('/\s(href¦src)=["\']?\/?(?!(https?:))([^>"\'\s]+)/i',' $1="../$3" class="imgtank',$row_page['content_'.$language]);$pattern="/<img src=\"([^\"]+)\" class=\"imgtank\" \/>/";
$replace="<a href=\"$1\" rel=\"lightbox[page]\"><img src=\"$1\" class=\"imgtank\" /></a>";
$deinhoud=preg_replace($pattern,$replace,$text);
echo $deinhoud;
?>
What have i learned?
Regular expression between () becomes a string.
How can you isolate that string? for example for resize use
getimagesize("$1");
Again, this only works if your code is uniform: i.e. if the image tags are all exactly the same, there's no additional spacing etc.