Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/145479-solved-auto-convert-urls-to-links/
Share on other sites

<?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.

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; ?>

 

 

 

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 :)

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.