0

I'd like to setup a function in Codeigniter that would turn URLs into active links. I found the function below here--it's for PHP generally, but need something similar for Codeigniter. Would like to use it throughout my site for user posts and comments.

$text = preg_replace('/(?<!http:\/\/)(www.[-a-zA-Z0-9@:%_\+.~#?&\/=]+)/i', '<a href="http://\1">\1</a>', $text);
Community
  • 1
  • 1
chowwy
  • 1,126
  • 8
  • 26
  • 45

5 Answers5

2

I ended up using auto_link() which is a function built into codeigniter. So if someone posts a message and you want to linkify any urls, simply use:

auto_link($message)

I found it buried in the codeigniter documentation.

chowwy
  • 1,126
  • 8
  • 26
  • 45
0

Consider URL Helper in CI. There are a couple of functions that assist in working with URLs. Take a look at the anchor() function specifically.

Taz
  • 3,718
  • 2
  • 37
  • 59
0

Create a file called MY_text_helper.php in application/helpers

put the following function in there:

function linkify($text){
    return preg_replace('/(?<!http:\/\/)(www.[-a-zA-Z0-9@:%_\+.~#?&\/=]+)/i', '<a href="http://\1">\1</a>', $text);
}

Now in your controller:

//$content = 'get your content from somewhere'
$this->load->helper('text');
$content = linkify($content);
Broncha
  • 3,794
  • 1
  • 24
  • 34
0

Make a helper and autoload it.

Matthew
  • 24,703
  • 9
  • 76
  • 110
0

Simply put preg_replace command into view your page like this: $ text = "visit the web http://masalahkita.com to see this work. Web is using CodeIgniter framework and apply this way."; $ link = preg_replace("/([\w]+://[\w-?&;#~=./\@]+[\w/])/i","$1", $text); echo $ link;

andy
  • 41
  • 1