summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/helpers/url_helper.php19
-rw-r--r--tests/codeigniter/helpers/url_helper_test.php11
2 files changed, 16 insertions, 14 deletions
diff --git a/system/helpers/url_helper.php b/system/helpers/url_helper.php
index a6536cf81..d0fab3fe0 100644
--- a/system/helpers/url_helper.php
+++ b/system/helpers/url_helper.php
@@ -384,22 +384,23 @@ if ( ! function_exists('auto_link'))
function auto_link($str, $type = 'both', $popup = FALSE)
{
// Find and replace any URLs.
- if ($type !== 'email' && preg_match_all('#\b(([\w-]+://?|www\.)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#', $str, $matches, PREG_OFFSET_CAPTURE))
+ if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
{
// Set our target HTML if using popup links.
- $target = ($popup) ? 'target="_blank"' : '';
+ $target = ($popup) ? ' target="_blank"' : '';
// We process the links in reverse order (last -> first) so that
// the returned string offsets from preg_match_all() are not
// moved as we add more HTML.
- foreach (array_reverse($matches[0]) as $match)
+ foreach (array_reverse($matches) as $match)
{
- // $match is an array generated by the PREG_OFFSET_CAPTURE flag.
- // $match[0] is the matched string, $match[1] is the string offset.
-
- $anchor = anchor($match[0], '', $target);
-
- $str = substr_replace($str, $anchor, $match[1], strlen($match[0]));
+ // $match[0] is the matched string/link
+ // $match[1] is either a protocol prefix or 'www.'
+ //
+ // With PREG_OFFSET_CAPTURE, both of the above is an array,
+ // where the actual value is held in [0] and its offset at the [1] index.
+ $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
+ $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
}
}
diff --git a/tests/codeigniter/helpers/url_helper_test.php b/tests/codeigniter/helpers/url_helper_test.php
index 5fc364238..24823a634 100644
--- a/tests/codeigniter/helpers/url_helper_test.php
+++ b/tests/codeigniter/helpers/url_helper_test.php
@@ -48,11 +48,12 @@ class Url_helper_test extends CI_TestCase {
public function test_auto_link_url()
{
$strings = array(
- 'www.codeigniter.com test' => '<a href="http://www.codeigniter.com">http://www.codeigniter.com</a> test',
+ 'www.codeigniter.com test' => '<a href="http://www.codeigniter.com">www.codeigniter.com</a> test',
'This is my noreply@codeigniter.com test' => 'This is my noreply@codeigniter.com test',
- '<br />www.google.com' => '<br /><a href="http://www.google.com">http://www.google.com</a>',
- 'Download CodeIgniter at www.codeigniter.com. Period test.' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">http://www.codeigniter.com</a>. Period test.',
- 'Download CodeIgniter at www.codeigniter.com, comma test' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">http://www.codeigniter.com</a>, comma test'
+ '<br />www.google.com' => '<br /><a href="http://www.google.com">www.google.com</a>',
+ 'Download CodeIgniter at www.codeigniter.com. Period test.' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">www.codeigniter.com</a>. Period test.',
+ 'Download CodeIgniter at www.codeigniter.com, comma test' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">www.codeigniter.com</a>, comma test',
+ 'This one: ://codeigniter.com must not break this one: http://codeigniter.com' => 'This one: <a href="://codeigniter.com">://codeigniter.com</a> must not break this one: <a href="http://codeigniter.com">http://codeigniter.com</a>'
);
foreach ($strings as $in => $out)
@@ -66,7 +67,7 @@ class Url_helper_test extends CI_TestCase {
public function test_pull_675()
{
$strings = array(
- '<br />www.google.com' => '<br /><a href="http://www.google.com">http://www.google.com</a>',
+ '<br />www.google.com' => '<br /><a href="http://www.google.com">www.google.com</a>',
);
foreach ($strings as $in => $out)