3

I want to add a rel=lightbox to every <a href=""> including an image .

<a href=""><img src="" alt="" title="" border="" /></a>

to

<a href="" rel="lightbox"><img src="" alt="" title="" border="" /></a>

And to make this i wrote the following php code :

$var1= $rows['article'];

 $var1= str_replace('<br>', '<br />', $var1);

 $var1 = str_replace('align="none"', '', $var1);
 $var1 = str_replace('target=""', 'target="_self"', $var1);

 $var2= preg_replace( '~<img\s[^>]*\K(?<!/)>~', "/>", $var1);

$var2= preg_replace('~<a(?=[^>]+>\s*<img)~','<a rel="lightbox"',$var2);

 echo $var2; 

The code before is doing the job, but in the case that i want to link an image not to an other image but to a url link then i have problem beacuse when is trying to open the url link actually the lightbox effect loads !

I want to modify it so when i link to a url link to dont apply the lightbox effect in this case.

But how can i make this, can you help me pleaaase ?

Thanks a lot

Nikos
  • 31
  • 3
  • Have a look into http://simplehtmldom.sourceforge.net - this would take the pain out of it! – benJ Mar 23 '15 at 13:12
  • Hi BenJ i dont want to use a HTML DOM parser to make that, I know using a dom parser is much more better solution but i want to make it done in the way i coded it as you can see in the snippet. Can we in some way detect (bmp|gif|jpeg|jpg|png) extentions into and if is not detected then to dont apply rel="lightbox" ? How can i make this ?? – Nikos Mar 23 '15 at 13:16
  • See this Q&A http://stackoverflow.com/q/173868/ you can further your research by using the following keywords "get file extension php". – Funk Forty Niner Mar 23 '15 at 13:28
  • Look at `pathinfo`. http://php.net/pathinfo – web-nomad Mar 23 '15 at 13:45

1 Answers1

1

Make an if statement that says your link has to have

<a 

and

<img 

in it to apply the rule.

Alex
  • 147
  • 1
  • 9