Forum Moderators: coopster

Message Too Old, No Replies

need to do some preg replace with <img src

         

ritz

3:37 pm on Jun 21, 2008 (gmt 0)

10+ Year Member



there's $row['content']

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]

Receptional Andy

4:09 pm on Jun 21, 2008 (gmt 0)



If the code is uniform, a simple approach would be to just capture the destination for the image and output this in both img src and href:

$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]

ritz

6:07 pm on Jun 21, 2008 (gmt 0)

10+ Year Member



Well, it worked! Thx for that.

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");

? coz that didnt work.

coopster

7:41 pm on Jun 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can use the "e" modifier [php.net] to evaluate the replacement string as PHP code. The preg_replace [php.net] manual page shows an example of how to use the "e" modifier. If you need to do a bit more work with the variables being captured in subpatterns, you can use preg_replace_callback [php.net].

Receptional Andy

8:16 pm on Jun 21, 2008 (gmt 0)



Two preg_replace calls are unnecessary since you can easily add the class with the output of the second call.

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.

ritz

10:34 pm on Jun 21, 2008 (gmt 0)

10+ Year Member



Thanks all.

I simplefied the code, we forget the string for this moment.

You where a great help.
thx again