Jump to content

[SOLVED] regex to replace img src with a href...


siwelis

Recommended Posts

I want to stop images from using bandwidth unless someone specifically decides to view the image... Whether it's png,PNG,svg,SVG,jpg,JPG,JPEG,jpeg,gif, or GIF...

 

While <img src=""> is always there, border, alt, and slash (/) are sometimes there.

 

E.g.

<?php
$content = 'So the most Christian image on the internet ever is <img src="http://chexed.com/images/rsymbols/Cross.gif" border="0" alt="Cross" /> but don't forget my logo <img src="http://chexed.com/images/logos/243x88.jpg"> which is also pretty cool, though not necessarily Christian.

//USE REGEX TO GET THE FOLLOWING OUTPUT

preg_replace(SOMETHINGhere,<a href="\0">Click to view image</a>, $content)
//pretty sure I have everything right except the SOMETHINGhere
?>

 

Output:

So the most Christian image on the internet ever is <a href="http://chexed.com/images/rsymbols/Cross.gif">Click to view image</a> but don't forget about my logo <a href="http://chexed.com/images/logos/243x88.jpg">Click to view image</a> which is also pretty cool, though not necessarily Christian.

 

I've been working on and off on this for days! I've read enough about regex to know it's possible, just haven't found the right article/page to teach me the syntax in its entirety except one for JavaScript regex, which I don't know if it's the same or not... Any help will be very much appreciated!

Link to comment
Share on other sites

<?php

$str = array(
'<img alt="test" src="someimg.png" title="test title" />',
'<img src="someimg.png">',
'<img src="someimg.png" />',
'<img alt="test" src="someimg.png" style="blah" title="test title" />',
);

foreach($str as $t) {
echo preg_replace('/<img.*src="(.*?)".*\/?>/', '<a href="\1">Image</a>', $t) . PHP_EOL;
}

/*output:
<a href="someimg.png">Image</a>
<a href="someimg.png">Image</a>
<a href="someimg.png">Image</a>
<a href="someimg.png">Image</a>
*/

?>

Link to comment
Share on other sites

Hello Corbin! Thank you so much!

 

Your code worked great for me... I just modified it a bit to make it work for my necessary application... I'm posting the modifications here for future users... I wrote something in bold at the bottom for you...

 

At first I was getting an error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '?' in

 

Apparently the second question mark didn't need to be escaped... I also took out the "foreach" and accompanying squiggly brackets since I didn't need the $str to be an array...

 

My final code was

preg_replace( '/<img.*src="(.*?)".*?>/', '<a href="\1">Image file</a>', $str );

 

No one ever says hi because we take the nature of computers (unresponsive without input, and responses are preprogrammed) and wrongfully apply them to people on the net, who's computer input is often the most we see of them anyway, making even their responses seem less human than computer. Currently, without AI, saying hi to a computer is like saying hi to a stone. You rarely get a response you don't expect... Luckily for us though, thanks to things like your signature "Why doesn't anyone ever say hi, hey, or whad up world?" we are reminded of our chance to be friendly, and that it should at least occasionally be acted on ^_~

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.