Jump to content

Creating clickable links from user comment, then do wordwrap and htmlentities


Recommended Posts

Hello, I did a search, I didn't find something similar...

I need a function, maybe someone can help.

 

I need to display a user comment (just like a forum post) and

 

- convert the plain text into a clickable html link

- do wordwrap on the visible text but not inside the link tag

- do a htmlentities or htmlspecialchars on the text outside of the link

 

This is exactly how Vbulletin posts are processed, liks are clickable and text is wrapped properly to avoid long texts that break site layout.

Anyone can help? I am sure some people used this in websites.

Thanks.

 

I already have many "user comments" in database and Is best to find a good way to display those instead of using something like BB code.

 

Maybe somone can tell me how to apply a htmlentities on text outside of the <a> tags for this function?

How can I do a htmlentities($1) and htmlentities($2) for this code?

 

<?php 
// converts links inside plain text to clickable <a> tags
if (!function_exists("text_to_clickable_link")) {
function text_to_clickable_link($text){ 
	# this functions deserves credit to the fine folks at phpbb.com 
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "$1:", $text); 

	// pad it with a space so we can match things at the start of the 1st line. 
	$ret = ' ' . $text; 

	// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing 
	// Must contain at least 2 dots. xxxx contains either alphanum, or "-" 
	// zzzz is optional.. will contain everything up to the first space, newline, 
	// comma, double quote or <. 
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href=\"http://$2\" rel=\"nofollow\" target=\"_blank\">$2</a>", $ret); 

	// Remove our padding.. 
	$ret = substr($ret, 1); 
	return $ret; 
} 
}
?>

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.