how do I convert data submitted by a form to HTML?
Moderator: General Moderators
how do I convert data submitted by a form to HTML?
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.
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.
regex
should work...
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);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
offcourse, there are a lot of similar packages available... just look them up and pick the one you like
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);
}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.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:
here's an example of what would be typed into the form:
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.
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);Code: Select all
їvideo]stulayї/video]Stu Layїend]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.
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:
then they can put:
[video="name"]text[/video]
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);[video="name"]text[/video]
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.
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.
sorry to bring back an old topic, but i've ran into another problem.
i'd like to have:
to replace URLs that are typed in
and
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
thanks for any help.
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);and
Code: Select all
$body = preg_replace("|\[link=[\"\']?(.+?)[\"\']?\](.+?)\[/link\]|i", "<a href=\"http://www.$1\" target=\"blank\">$2</a>", $body);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
thanks for any help.