obay Posted November 17, 2008 Share Posted November 17, 2008 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? Quote Link to comment Share on other sites More sharing options...
obay Posted November 17, 2008 Author Share Posted November 17, 2008 nevermind, i got it! $newStr = preg_replace("/(<img[^>]*.{1})/", "<a>$1</a>", $str); i just put the pattern inside () and changed \\1 into $1 lol Quote Link to comment Share on other sites More sharing options...
ddrudik Posted November 17, 2008 Share Posted November 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
obay Posted November 18, 2008 Author Share Posted November 18, 2008 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! Quote Link to comment Share on other sites More sharing options...
ddrudik Posted November 18, 2008 Share Posted November 18, 2008 Those were just various code examples for the task at hand. For your last question: $newStr = preg_replace('#<img src=("[^"]*") [^>]+>#', '<a href=$1>$0</a>', $str); Quote Link to comment Share on other sites More sharing options...
obay Posted November 19, 2008 Author Share Posted November 19, 2008 super!!!! man, you're good!!!! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 19, 2008 Share Posted November 19, 2008 That one would only work if src was the first attribute. How about: $newStr = preg_replace('#<img (.*? )?src=("[^"]*") [^>]+>#', '<a href=$1>$0</a>', $str); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.