Adding a \ to an <img tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Adding a \ to an <img tag

Post by Grim... »

Okay, if my users are inserting some text, and they want to add an image, they can just do it with html, ie:

Code: Select all

<img src="bob.jpg">
How can I automatically go through their post string and make that into

Code: Select all

<img src="bob.jpg" \>
I assume it's to do with preg_something but I'm no good at that :(

Also, it there a way to add a default alt tag if it hasn't already got one, ie:

Code: Select all

<img src="bob.jpg" alt="Image" \>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Okay, I sussed the first bit out myself, it's

Code: Select all

<?php
$string = preg_replace("/<img(.+?)>/", "<img $1 />", $string);
?>
Post Reply