How to parse images with [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
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

How to parse images with [img] tag?

Post by anivad »

I've been trying to enable image embedding for a forum; currently when users submit a post ($epost), it goes through this:

Code: Select all

$epost = mysql_real_escape_string($epost);
$epost = htmlspecialchars($epost);
 
$epost = eregi_replace('(\\\')', ''', $epost);
$epost = eregi_replace('(\[b\])', '<b>', $epost);
$epost = eregi_replace('(\[/b\])', '</b>', $epost);
$epost = eregi_replace('(\[i\])', '<i>', $epost);
$epost = eregi_replace('(\[/i\])', '</i>', $epost);
$epost = eregi_replace('(\[u\])', '<u>', $epost);
$epost = eregi_replace('(\[/u\])', '</u>', $epost);
$epost = preg_replace('@(?<!")(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)(?!")@', '<a href="$1">$1</a>', $epost);
$epost = eregi_replace('<a href="', '', $epost);
$epost = eregi_replace('</a>', '', $epost);
Which parses any URLs.

When an image is posted, I replace the [img]tag%20with%20<img%20src='%20and[/img] with '> on the display side.

But it also gets its URL parsed such that the final result goes:

<img src='<a href='http://picture.jpg'>http://picture.jpg</a>'>

How do I get out of that?

Thanks!
Post Reply