We might have seen plain text get converted to hypertext when we type it. For example, In gtalk when we type www.google.com it automatically changes as a hyperlink and point to http://www.google.com, Same way, when we type an email address it is automatically converted to mailto:email@example.com.
These kind of conversion can be made using PHP string replace function. We have to check the whole string where it has the url or email address, then we have to replace the url with anchor tag.
In CodeIgniter the easiest way to do it is to use the URL helper. First step is to load the URL helper.
Then the next thing to do is to use a function
This function makes our life easier. We have to pass the string that we need to find for url and email.
$string = "Today I visited a useful blog about web development and its url is www.geeks.gallery.";
The output of the above code is Today I visited a useful blog about web development and its url is www.geeks.gallery
The same way email id is also auto linked. We can also restrict the automation only to url or email. This can be done by
In case if we want the link to open in a new tab, we can do it by adding a third parameter which can be either TRUE or FALSE.
echo auto_link($string, 'both', TRUE);
Here second parameter ‘both’ indicates that both ’email’ and ‘url’ has to be automatically converted to hypertext.
Thanks for reading.
Latest posts by Nagarajan (see all)