Skip to content

Commit 0df7832

Browse files
committed
Simplified plugin to use the auto_link() function from CodeIgniter's URL helper.
Also added new optional parameter to choose to auto-link only URLs or email addresses.
1 parent a5f35f4 commit 0df7832

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

auto_linker/pi.auto_linker.php

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22

33
/*
4-
Copyright (C) 2004 - 2011 EllisLab, Inc.
4+
Copyright (C) 2004 - 2012 EllisLab, Inc.
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -26,11 +26,11 @@
2626
*/
2727

2828
$plugin_info = array(
29-
'pi_name' => 'URL and Email Auto-linker',
30-
'pi_version' => '1.1',
31-
'pi_author' => 'Rick Ellis',
32-
'pi_author_url' => 'http://www.expressionengine.com/',
33-
'pi_description' => 'Automatically links URLs within text',
29+
'pi_name' => 'Auto Linker',
30+
'pi_version' => '2.0',
31+
'pi_author' => 'EllisLab',
32+
'pi_author_url' => 'http://www.ellislab.com/',
33+
'pi_description' => 'Automatically creates links from URLs and/or email addresses contained within the given text.',
3434
'pi_usage' => Auto_linker::usage()
3535
);
3636

@@ -58,32 +58,19 @@ class Auto_linker {
5858
function Auto_linker($str = '')
5959
{
6060
$this->EE =& get_instance();
61+
62+
$this->EE->load->helper('url');
6163

6264
$str = ($str == '') ? $this->EE->TMPL->tagdata : $str;
63-
64-
$pop = ($this->EE->TMPL->fetch_param('target') == 'blank') ? " target=\"_blank\" " : "";
65-
66-
// Clear period from the end of URLs
67-
$str = preg_replace("#(^|\s|\()((http://|https://|www\.)\w+[^\s\)]+)\.([\s\)])#i", "\\1\\2{{PERIOD}}\\4", $str);
6865

69-
// Auto link URL
70-
$str = preg_replace("#(^|\s|\(|>)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", "\\1<a href=\"http\\4://\\5\\6\"$pop>http\\4://\\5\\6</a>", $str);
71-
72-
//$str = preg_replace("#(^|\s|\(|..\])((http(s?)://)|(www\.))(\w+[^\s\)\<\[]+)#im", "\\1<a href=\"http\\4://\\5\\6\"$pop>http\\4://\\5\\6</a>", $str);
66+
// Parameter to determine whether to convert only
67+
// URLs, email addresses, or both.
68+
$convert = ($this->EE->TMPL->fetch_param('convert') == 'url' OR $this->EE->TMPL->fetch_param('convert') == 'email') ? $this->EE->TMPL->fetch_param('convert') : 'both';
7369

70+
// Parameter to determine whether link opens in a new window.
71+
$pop = ($this->EE->TMPL->fetch_param('target') == 'blank') ? TRUE : FALSE;
7472

75-
76-
// Clean up periods
77-
$str = preg_replace("#<a href=(.+?){{PERIOD}}(.+?){{PERIOD}}</a>#", "<a href=\\1\\2</a>.", $str);
78-
79-
// Clear period from the end of emails
80-
$str = preg_replace("#(^|\s|\(|>)([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)\.([\s\)])#i","\\1\\2@\\3.\\4\\5{{PERIOD}}",$str);
81-
82-
// Auto link email
83-
$str = preg_replace("/(^|\s|\(|>)([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", "\\1<a href=\"mailto:\\2@\\3.\\4\">\\2@\\3.\\4</a>", $str);
84-
85-
// Cleand up stray periods
86-
$str = str_replace(" {{PERIOD}}", ". ", $str);
73+
$str = auto_link($str, $convert, $pop);
8774

8875
$this->return_data = $str;
8976
}
@@ -102,20 +89,45 @@ function usage()
10289
{
10390
ob_start();
10491
?>
105-
Auto-links URLs
92+
=====================================================
93+
Example
94+
=====================================================
10695

107-
Wrap whatever you want formatted within:
96+
Wrap the text to be formatted within the plugin tags, like so:
10897

10998
{exp:auto_linker}
11099

111100
text you want processed
112101

113102
{/exp:auto_linker}
114103

104+
or...
105+
106+
{exp:auto_linker}
115107

116-
There is one optional parameter which will option links in a new window.
108+
{custom_field}
109+
110+
{/exp:auto_linker}
111+
112+
113+
Note: Mailto links created for email addresses use an obfuscated version of the mailto tag containing ordinal numbers written with JavaScript to help prevent the email address from being harvested by spam bots.
114+
115+
=====================================================
116+
Optional Parameters
117+
=====================================================
117118

118119
target="blank"
120+
- Links will open in a new window.
121+
122+
convert=""
123+
- Set to "url" to only convert URLs.
124+
- Set to "email" to only convert email addresses.
125+
126+
127+
Version 2.0
128+
******************
129+
- Simplified plugin to use the auto_link() function from CodeIgniter's URL helper.
130+
- New optional parameter to choose to auto-link only URLs or email addresses.
119131

120132
Version 1.1
121133
******************

0 commit comments

Comments
 (0)