php - Adding link tags and rel="lightbox" around images Regex -
how can change example when got <image>
tag , want add link tags around them , rel lightbox attribute well? cant figure out. i'm not regular expressions @ all.
the example
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
so in case have <img src="...." class="...." alt=".....">
and need <a href="....." rel="lightbox" title="....."><img src="...." class="...." alt="....."></a>
how change this? help.
it's unclear want here, utilize dom task instead.
$doc = domdocument::loadhtml(' <img src="www.foo.com/1.gif" class="foo" alt="..."> <img src="www.bar.com/1.jpg" class="bar" alt="..."> <img src="example.com/2.jpg" class="example" alt="..."> '); foreach ($doc->getelementsbytagname('img') $node) { $link = $node->ownerdocument->createelement('a'); $a = $node->parentnode->insertbefore($link, $node); $a->setattribute('href', $node->getattribute('src')); $a->setattribute('rel', 'lightbox'); $a->setattribute('title', 'some title'); $a->appendchild($node); } echo $doc->savehtml();
output:
<a href="www.foo.com/1.gif" rel="lightbox" title="some title"><img src="www.foo.com/1.gif" class="foo" alt="..."></a> <a href="www.bar.com/1.jpg" rel="lightbox" title="some title"><img src="www.bar.com/1.jpg" class="bar" alt="..."></a> <a href="example.com/2.jpg" rel="lightbox" title="some title"><img src="example.com/2.jpg" class="example" alt="..."></a>
Comments
Post a Comment