htmlstig Posted February 16, 2009 Share Posted February 16, 2009 hi i have made a simple guestbook and before it sends the message to the database i have used trim($comment) and strip_tags($comment) so that it removes all php,html etc coding as the spammers were all using html for their links instead of just starting http:// the downside to that is that people can no longer post links so i was wondering is it possible to use php so where ever someone starts with http:// or www it converts that url to a link? if so how? cheers Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 16, 2009 Share Posted February 16, 2009 $http = "http://"; $www = "www"; $link = "some link"; if(strstr($link,$http) || strstr($link,$www)) { $new_link = "http://www" . $link; } Quote Link to comment Share on other sites More sharing options...
premiso Posted February 16, 2009 Share Posted February 16, 2009 <?php $string = "This should be converted to http://link.com www.link.com a link and http://www.link.com ok! and www.link.com "; $replace = array('~\shttp://(.+?)\s~', '~\swww\.(.+?)\s~'); $replaceWith = array(' <a href="http://$1">http://$1</a> ', ' <a href="http://www.$1">www.$1</a> '); $string = preg_replace($replace, $replaceWith, $string); echo $string; ?> Not perfect, but should work. Quote Link to comment Share on other sites More sharing options...
htmlstig Posted February 16, 2009 Author Share Posted February 16, 2009 for some reason it didnt work i tried it before it sends to database and when getting it from database ... this is how i had it before sending to database //get details from submitted form $name=$_POST['name']; $email=$_POST['email']; $website=$_POST['website']; $comment=$_POST['comment']; //remove any html,php,javascript e.t.c from comment $comment = trim($comment); $comment = strip_tags($comment); //convert urls to links $replace = array('~\shttp://(.+?)\s~', '~\swww\.(.+?)\s~'); $replaceWith = array(' <a href="http://$1">http://$1</a> ', ' <a href="http://www.$1">www.$1</a> '); $comment = preg_replace($replace, $replaceWith, $comment); getting from database <?php $comment = $row['comment']; $replace = array('~\shttp://(.+?)\s~', '~\swww\.(.+?)\s~'); $replaceWith = array(' <a href="http://$1">$1</a> ', ' <a href="http://$1">$1</a> '); $comment = preg_replace($replace, $replaceWith, $comment); ?> then where i wanted the comment i put <? echo $comment; ?> Quote Link to comment Share on other sites More sharing options...
gevans Posted February 17, 2009 Share Posted February 17, 2009 Either do it before you add to the databse, or when you retrieve, not both Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 The best way would be when you insert in in the database ... becase to much operations (Ex: bbcode) when printing out the code slows the page when you have a lot of comments to show. Quote Link to comment Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 The best way would be when you insert in in the database ... becase to much operations (Ex: bbcode) when printing out the code slows the page when you have a lot of comments to show. I would disagree, either way the processing is there. But storing it in it's raw form allows for any type of manipulation. That is just my 2cents though. Do it when it is coming out of the DB. Not going in. @OP, why it does not work I do not know, could be that you were double using it/using it wrong. It could also be that you put the link at the end IE: "This text link will not be processed http://www.link.com" because there is no ending space, as the regex uses the space to determine the end of a link. How to avoid this, I am not sure. You can always just append a space to the comment variable and that would solve that issue. Same if the link is at the beginning of the string. Like I said, not perfect Quote Link to comment Share on other sites More sharing options...
htmlstig Posted February 17, 2009 Author Share Posted February 17, 2009 Either do it before you add to the databse, or when you retrieve, not both i havnt done it both at same time they are both ways i have tried it The best way would be when you insert in in the database ... becase to much operations (Ex: bbcode) when printing out the code slows the page when you have a lot of comments to show. I would disagree, either way the processing is there. But storing it in it's raw form allows for any type of manipulation. That is just my 2cents though. Do it when it is coming out of the DB. Not going in. @OP, why it does not work I do not know, could be that you were double using it/using it wrong. It could also be that you put the link at the end IE: "This text link will not be processed http://www.link.com" because there is no ending space, as the regex uses the space to determine the end of a link. How to avoid this, I am not sure. You can always just append a space to the comment variable and that would solve that issue. Same if the link is at the beginning of the string. Like I said, not perfect yeah the links i tried were first and last ill try it again so that the links arnt first or last Quote Link to comment Share on other sites More sharing options...
htmlstig Posted February 17, 2009 Author Share Posted February 17, 2009 sorted cheers when the link wasnt first or last it worked so i will have to make sure it adds a space at the begining and end of the message before it sends to the database Quote Link to comment Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 The best way would be when you insert in in the database ... becase to much operations (Ex: bbcode) when printing out the code slows the page when you have a lot of comments to show. I would disagree, either way the processing is there. But storing it in it's raw form allows for any type of manipulation. That is just my 2cents though. Do it when it is coming out of the DB. Not going in. But when you enter it to the database your actualy processing only one ... but when you output it you output 10 or 20 Quote Link to comment Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 But when you enter it to the database your actualy processing only one ... but when you output it you output 10 or 20 Understandable, but it all depends on the functionality. Like I said it will limit your functionality. Sort of, why even store it as BBCode in the database? Or why not just convert \n's to <br /> before entering into a DB? The reason being is you want it in it's raw format so you can manipulate it. Same rule applies here. That and the processing time really is not that much extra at all for the added functionality of the data stored in the DB. EDIT: And to be honest it is much more than 10 or 20 times. Depending on how many people view the site each day, it could be thousands. But I would still do it that way. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.