1

I am allowing people to post comments using a textarea field and sometimes they post urls. What I need to do is to convert this url from db before displaying it as a real clickable link, but without allowing html tags. I would prefer to do it using php or jquery if possible. I thought about using something like [link][/link] but I need to do it without any extra effort from the website member. Any ideas please??

example :

[link]http://www.google.com[/link] 
2

4 Answers 4

1

Here is a little PHP script that I have written. It seems to work for me. It uses the preg_match_all and preg_replace methods to match all the links inserted by the end user with <a> tags.

<?php $text="Click [link]http://www.google.com[/link] or click [link]http://www.yahoo.com[/link]"; preg_match_all('/\\[link](.*?)\\[\/link]/s', $text, $links); $link_count=count($links); for($i=0;$i<$link_count;$i++){ $link_url=preg_replace("/\[link]/", "", $links[0][$i]); $link_url=preg_replace("/\[\/link]/","",$link_url); $text=str_replace($links[0][$i],"<a href=\"" . $link_url . "\">" . $link_url . "</a>",$text); } echo $text; ?> 
Sign up to request clarification or add additional context in comments.

Comments

0

use regex to find urls within the block of text, then append and prepend the necessary tags

http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/

Comments

0

You may or may not be interested in this software: http://markitup.jaysalvat.com/examples/bbcode/

Comments

0

You can use preg_replace method:

//URL's $pattern = "/\[link\=(.*)\](.*)\[\/link\]/i"; $replace = "<a href=\"$1\">$2</a>"; echo preg_replace($pattern, $replace, $subject); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.