Page 1 of 1

How to handle posted HTML and URLS

Posted: Mon May 07, 2007 4:33 pm
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!

Posted: Tue May 08, 2007 6:15 am
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);

Posted: Tue May 08, 2007 6:53 am
by superdezign
html_special_chars may be of interest. It'll handle everything aside from the URLs automatically becoming links.

Posted: Tue May 08, 2007 7:10 am
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()...

Posted: Tue May 08, 2007 7:13 am
by Grim...
That would probably be useful if I didn't just copy it straight out of the manual comments :)