1

Using auto_link() to output the copy from a CMS controlled page onto the front end. I have 2 email addresses, recruit@ and bankrecruit@ in the stored copy.

When I look at the front end the first email, recruit@, is auto_linked to become a linked email address but the second one becomes bank followed by the recruit@ email link. This is obviously not what I expected.

auto_link() is matching all cases of recruit@ in which case the bankrecruit@ is being converted as it finds recruit@ first and converts that.

If I remove the recruit@ then bankrecruit@ works fine. Also if I change the name to bank@ then both addresses work as expected.

Is there a resolution for this?

<p>This is the address a@test.com</p>
<p>This is the second address ba@test.com</p>

And the script is:

auto_link($content)
rmccallum
  • 214
  • 3
  • 18
  • echo auto_link($page_content->page_body); – rmccallum Jan 15 '13 at 13:51
  • And this is the content:

    This is the address a@test.com

    This is the second address ba@test.com

    – rmccallum Jan 15 '13 at 13:57
  • In that content a@test.com is converted to email links twice leaving b as standard text – rmccallum Jan 15 '13 at 13:57
  • 1
    Your code should work as documented, but it's an obvious bug in CI where the replacement is not properly being done where a replace string matches a substring of another replace string you get your outcome. Please open a bug report here: https://github.com/EllisLab/CodeIgniter/issues/new – kittycat Jan 15 '13 at 14:21

1 Answers1

1

As @cryptic pointed, it is a bug in the auto_link method. (See source) They are finding all email address in the output and then, they do a replace all (str_replace) with the anchored version. So...

<p>This is the address a@test.com</p>
<p>This is the second address ba@test.com</p>

becomes

<p>This is the address <a ...>a@test.com</a></p>
<p>This is the second address b<a ...>a@test.com</a></p>

on first pass for the email a@test.com. On the second e-mail, they try to replace ba@test.com with the anchored version, but the str_replace can not find the address, it has already been replaced.

You could implement your own fix by :

  1. Extending the auto_link method of the URL helper. See documentation
  2. Copy the auto_link method from the CodeIgniter source into that new Helper.
  3. Replace only the first occurrence of the string. See this SO thread.

For example:

$str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);

becomes

$str = preg_replace('/' . $matches['0'][$i] . '/', safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str, 1);

That should fix it for you. I would advise against modifying the system's URL_Helper, you might run into some migration problems later on.

Hope this helps.

Community
  • 1
  • 1
Maxime Morin
  • 2,008
  • 1
  • 21
  • 26