NameSilo

Small PHP preg_replace question.

SpaceshipSpaceship
Watch

agasonex

Established Member
Impact
1
Hi, i've never been too good with preg_replaces, and was wondering what I would use to replace
<img src="imageurl" border="0">
with
<a href="imageurl">imageurl</a>

Thanks for any help, will give rep. =)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
<?php
$string="<img src=\"imageurl\" border=\"0\">";

$new = explode('"',$string);
$new_string = "<a href=\"$new[1]\">$new[1]</a>";

echo $new_string;
?>

tested and works (w/o using preg_replace tho)
 
0
•••
Thanks for the help, but it doesn't quite work with what I'm trying to do. Sorry about that, I should have been more specific.

I'm trying to get it on my forum so users can choose to have images replaced with a link to the image.
So it would have to be able to replace all of the "<img src="~~" of a text field.

Gave you a rep+ anyway though.
 
0
•••
Tested this, and it works:
PHP:
<?php

$blah = '<img src="blahblah.gif" border="0">';

preg_match('/<img src="(.*?)" border="0">/si', $blah, $matches);
$new = preg_replace('@<img src=".*?" border="0">@si', '<a href="'.$matches[1].'">'.$matches[1].'</a>', $blah);

echo $new;

?>
 
0
•••
Thanks, works perfectly.
 
0
•••
No problem, glad to help :)
 
0
•••
I hope that when I grow up, I'll be as smart as SecondVersion :p
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back