EllisLab/CodeIgniter — pull request #2164 by cryode (2013-01-18 00:28:28+00:00)
Title: Fix and optimize auto_link() URL helper function.

Utilizes a fix for #2160 and #2161, and elaborates / optimizes:

- Uses the `PREG_OFFSET_CAPTURE` and `substr_replace()` solution for URLs, also, since they are susceptible to the same `str_replace()` bug.
- Uses a better regular expression for URLs, and optimize the regex for emails.
- Uses an easier to read `foreach()` loop instead of `for()`, and forgoes the use of the ugly `$matches[1][$i][1]` style variable. Also commented throughout to explain what the function is doing for easy reference.
- Unnecessary punctuation `preg_match()` removed.
- URL replace uses the `anchor()` function to generate the URL instead of by hand (why reinvent the wheel?).
- About 25% faster according to a real quick, basic benchmark test.

**Known caveat / bug:** The URL regex will match an FTP URL, such as `ftp://username:password@example.com`, but if the email auto link is turned on, it will also match the `password@example.com` portion, and the output will be all garbled with the javascript from `safe_mailto()`. I couldn't figure out a good way to prevent that without a bunch of extra code.

Signed-off-by: Eric Roberts