Page 1 of 1

how do I convert data submitted by a form to HTML?

Posted: Fri May 13, 2005 2:52 pm
by jrucifer
I'm creating a blog-style website which will only have a few members that update. I've set up all my databases, forms, and pages... but I'd like to be able to simply the update process for my members. First off, if someone were to post a link, i'd like for it to automatically add the html. so when they type:

http://www.website.com

it converts it to this when its submitted:

<a href="http://www.website.com" target="blank">http://www.website.com</a>

also, i'd like to have something like BBCode, where users can use simple tags which would convert to html. I'd appreciate any respone, and sorry if I sound like an idiot, I'm pretty new to PHP.

Posted: Fri May 13, 2005 3:45 pm
by Skara
regex

Code: Select all

$text = preg_replace( "`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>", $text);
should work...

Posted: Fri May 13, 2005 3:52 pm
by timvw
a possible solution is the http://pear.php.net/package/HTML_BBCodeParser

offcourse, there are a lot of similar packages available... just look them up and pick the one you like :)

Posted: Sat May 14, 2005 7:26 pm
by s.dot

Code: Select all

function make_clickable($text) 
{ 

   $ret = ' ' . $text; 
   $ret = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); 
   $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret); 
   $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); 
   $ret = substr($ret, 1); 
   return($ret); 
}
The above function is from PHPBB and will make any links including http://, http://www., ftp://, and mailto.

As far as the rest of the HTML goes. You could allow them to enter HTML. Or if you're not secure with that. Create your own tags using str_replace. For example

Code: Select all

// Creating a bold text
$blog = mysql_real_escape_string(strip_tags($_POST['blog']));
$blog2 = str_replace("[b]","<B>", $blog);
$blog3 = str_replace("[/b]","</B>", $blog2);

// you could create an unlimited number of BB style tags.

Posted: Thu May 19, 2005 3:08 am
by jrucifer
easier than i thought. thanks guys for all your help.

edit:
eh, scratch that. it's not quite workin out for me. here's my code:

Code: Select all

$body = str_replace("[video]","<a href=\"javascript:video('video.php?name=", $body);
$body = str_replace("[/video]","')\">", $body);
$body = str_replace("[end]","</a>", $body);
here's an example of what would be typed into the form:

Code: Select all

&#1111;video]stulay&#1111;/video]Stu Lay&#1111;end]
and here's the error i keep getting:

Database ERROR: INSERT INTO home (subject, body, date) VALUES ('test', 'Stu Lay', NOW()) You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'video.php?name=stulay')">Stu Lay', NOW())' at line 1

Once again, any help is greatly appreciated.

Posted: Thu May 19, 2005 11:09 am
by Skara
you can't have ' in the syntax. Escape them.
as for the [video] code, that's a really bad way to do it. People could just put [video][video][video]..etc.
change it to:

Code: Select all

$body = preg_replace('|\[video=["\']?(.+?)["\']?\](.+?)\[/video\]|i','<a href="javascript:video(\'video.php?name=\\1\');">\\2</a>',$body);
then they can put:
[video="name"]text[/video]

Posted: Thu May 19, 2005 12:26 pm
by jrucifer
thanks, that method is much cleaner... except i'm still getting almost the same exact error.

Database ERROR: INSERT INTO home (subject, body, date) VALUES ('test', 'text', NOW()) You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '?name=stulay')">text', NOW())' at line 1

any ideas?

edit:
nevermind, i got it figured out. thanks for all the help.

Posted: Wed Jun 01, 2005 12:14 am
by jrucifer
sorry to bring back an old topic, but i've ran into another problem.

i'd like to have:

Code: Select all

$body = preg_replace("`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http$3://$5$6$8$9\" target=\"blank\" title=\"$0\">$5$6</a>", $body);
to replace URLs that are typed in

and

Code: Select all

$body = preg_replace("|\[link=[\"\']?(.+?)[\"\']?\](.+?)\[/link\]|i", "<a href=\"http://www.$1\" target=\"blank\">$2</a>", $body);
so that they can have the link appear as text...

however, when the 2nd function is used, the URL that is submitted gets replaced by the first function. not sure if i explained that right, but i hope you will figure it out.

so, what do i need to do in order to use both without any conflicts? I promise I tried to figure it out myself before running here :roll:

thanks for any help.