How to handle posted HTML and URLS

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
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

How to handle posted HTML and URLS

Post by idotcom »

Hello,

I've been trying to figure out the best solution for what I need to do. I think it's simple enough, just need direction.

Ultimately, I need to handle posted info just like these forums.

What I need to do is this:

When a user posts to my site, I need:

1. Any HTML code to be visible as code.

2. Urls to be converted to clickable links.

3. Safely allow single and double quotes.

I've been using htmlentities, and htmlspecialchars, but I'm not getting the right result.

Any help would be greatly appreciated. This is already eating up too much time.

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

Post by Grim... »

1.

Code: Select all

$submission = str_replace("<", "<", str_replace (">", ">", $submission));
2.

Code: Select all

$submission = ereg_replace('[a-zA-Z]+://([.]?[a-zA-Z0-9_/?:#+=&-])*', "<a href=\"\\0\">\\0</a>", $submission );
$submission = ereg_replace('(^| )(www([.]?[a-zA-Z0-9_/?:#+=&-])*)', "\\1<a href=\"http://\\2\">\\2</a>", $submission );
3.

Code: Select all

$submission = addslashes($submission);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

html_special_chars may be of interest. It'll handle everything aside from the URLs automatically becoming links.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

To quote the php manual...
...preg_replace() which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace()...
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

That would probably be useful if I didn't just copy it straight out of the manual comments :)
Post Reply