PDA

View Full Version : User Input, Converting Links, HTML, Etc


sysera
11-18-2004, 04:21 AM
Hey guys,
I'm still working on some code here to take a users HTML form input on my website in several places; chat, forum, etc, and strip unwanted HTML, add HTML tags here and there, etc.

I could use a hand with making it a little bit more functional if anyone would like to help out. There really aren't any great "user input" classes out there that I have found, so I'd like to make this one work well for general use. This way everyone on the site here would be able to find one to hack up for their own uses easily. :)

So far it will take URLs and convert them to links that will open in new windows. I have also added a few things to add HTML tags to input, for bold, italics and centering. I would like it to be able to:

1. automatically take the URL of an image, and add IMG tags to it.

2. Be able to do URLs links with a link name specified. So you see whatever the user wants to display for the HTML link instead of the complete URL.

Here is what I have so far:

_____________START______________

$newcomment = stripslashes($_POST['newcomment']);
$newcomment = wordwrap($newcomment, 100, ' ', 1);
$search = array ("'<script[^>]*?>.*?</script>'si",
"'<[\/\!]*?[^<>]*?>'si",
"'([\r\n])[\s]+'",
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e");
$replace = array ("",
"",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$newcomment = preg_replace($search, $replace, $newcomment);
$newcomment = preg_replace( "/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $newcomment );
$newcomment = str_replace("{b}","<b>",$newcomment);
$newcomment = str_replace("{/b}","</b>",$newcomment);
$newcomment = str_replace("{i}","<i>",$newcomment);
$newcomment = str_replace("{/i}","</i>",$newcomment);
$newcomment = str_replace("{center}","<div align=middle>",$newcomment);
$newcomment = str_replace("{/center}","</div>",$newcomment);

______________FINISH___________

-Sys

scoutt
11-18-2004, 09:10 AM
for one you have a problem

align=middle

should be align=center ;)

also you will have to make it so the link has a replacement also. {url} nad {/ur} with {url=http://....}name{/ur}

you will have to check for both types. for the img you have to check for <img

sysera
11-19-2004, 03:24 AM
Originally posted by scoutt
for one you have a problem

align=middle

should be align=center ;)

also you will have to make it so the link has a replacement also. {url} nad {/ur} with {url=http://....}name{/ur}

you will have to check for both types. for the img you have to check for <img

Haha, woops! Looks like I have some work to do. ;)

Thanks for the looksie Scoutt.