Jump to content

[SOLVED] using reg expr, how do i wrap multiple <a> and </a> around multiple <img /> ?


obay

Recommended Posts

how do i wrap multiple <a> and </a> around multiple <img /> using regular expressions?

 

for example:

 

i want

 

<img src="image1" width="1" height="1" />
<img src="image2" width="1" height="2" />
<img src="image3" width="1" height="3" />

 

to become

 

<a><img src="image1" width="1" height="1" /></a>
<a><img src="image2" width="1" height="2" /></a>
<a><img src="image3" width="1" height="3" /></a>

 

i tried this:

$newStr = preg_replace("#(<img[^.]+>)#", "<a>\\1</a>", $str);
echo htmlentities($newStr);

with $str being the code with the three <img>'s

 

but it generates:

<a><img src="image1" width="1" height="1" />
<img src="image2" width="1" height="2" />
<img src="image3" width="1" height="3" /></a>

 

that is, only one set of <a></a>

 

help?

That may seem like the solution, but it's important to understand why it worked and why the pattern you selected as the solution is less preferable to a variation of the original pattern.

 

Consider that you want to match an img tag that starts with '<img' and ends with '>' so the pattern would be <img[^>]*> although to be careful you would add the i modifier to the pattern to handle case differences and you might want to add \s* after the '<' in the pattern to handle and errant whitespace in the tag.

 

<pre>
<?php
$str='<img src="image1" width="1" height="1" />
<img src="image2" width="1" height="2" />
<img src="image3" width="1" height="3" />';
$newStr = preg_replace("#(<img[^>]+>)#", '<a>\\1</a>', $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#(<img[^>]+>)#", "<a>$1</a>", $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#(<img[^>]+>)#", '<a>$1</a>', $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#<img[^>]+>#", "<a>\\0</a>", $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#<img[^>]+>#", '<a>\0</a>', $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#<img[^>]+>#", "<a>$0</a>", $str);
echo htmlentities($newStr);
echo '<hr>';
$newStr = preg_replace("#<img[^>]+>#", '<a>$0</a>', $str);
echo htmlentities($newStr);
echo '<hr>';
?>

 

The double-quotes in your preg_replace statement for the replacement pattern were causing it to be evaluated by PHP in a way that was breaking your backreference to capture group 1.  Note the different variations of single-quotes, double-quotes, and backreference styles that worked with preg_replace above.  Also note that capture group 0 is always available so there's no need to have a capture group 1 if you just want a backreference.

im sorry im not sure i understand..

 

i stumbled into another problem though.. i need to be able to "copy" the value in the <img>'s src, into the <a>'s href.. for example:

 

<img src="image1.jpg" width="1" height="1" />
<img src="image2.jpg" width="1" height="2" />
<img src="image3.jpg" width="1" height="3" />

 

into

 

<a href="image1.jpg"><img src="image1.jpg" width="1" height="1" /></a>
<a href="image1.jpg"><img src="image2.jpg" width="1" height="2" /></a>
<a href="image1.jpg"><img src="image3.jpg" width="1" height="3" /></a>

 

is this possible with regular expressions? if so, how do i do so?

 

thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.