diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index 1d03132d82cf..d68fea2aad7d 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -351,7 +351,7 @@ function safe_mailto(string $email, string $title = '', $attributes = ''): strin function auto_link(string $str, string $type = 'both', bool $popup = false): string { // Find and replace any URLs. - if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { // Set our target HTML if using popup links. $target = ($popup) ? ' target="_blank"' : ''; diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 125356e7d0ed..219b5322dbb5 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -533,6 +533,10 @@ public static function provideAutoLinkUrl(): iterable 'Visit www.example.com or email foo@bar.com', 'Visit www.example.com or email foo@bar.com', ], + 'trailing slash' => [ + 'Trailing slash: https://codeigniter.com/ fubar', + 'Trailing slash: https://codeigniter.com/ fubar', + ], ]; } @@ -629,6 +633,10 @@ public static function provideAutolinkBoth(): iterable 'Visit www.example.com or email foo@bar.com', "Visit www.example.com or email ", ], + 'email starting with "www."' => [ + 'this is some text that includes www.email@domain.com which is causing an issue', + 'this is some text that includes ' . safe_mailto('www.email@domain.com') . ' which is causing an issue', + ], ]; } diff --git a/user_guide_src/source/changelogs/v4.5.5.rst b/user_guide_src/source/changelogs/v4.5.5.rst index f6799e484ebf..aaea324399af 100644 --- a/user_guide_src/source/changelogs/v4.5.5.rst +++ b/user_guide_src/source/changelogs/v4.5.5.rst @@ -30,6 +30,10 @@ Deprecations Bugs Fixed ********** +- **URL Helper:** The bug where the regular expression in :php:func:`auto_link()` + was old has been fixed. As a result of this fix, the same regular expression as + CodeIgniter 3 is now used. + See the repo's `CHANGELOG.md `_ for a complete list of bugs fixed.