I wanted to pass data that contains url
within it, and the url
will be converted to click-able links, but the issue is that when i run the function
, it reiterate 3 times and the data is treated 3 times. How can I have a single data output? I tried removing the concatenation .=
to just =
but then, only the last pattern is being treated. I want it in 3 pattern because I want to add http
to the href
when a user input is www.ivotism.com
, it will be <a href="http//www.ivotism.com">www.ivotism.com</a>
instead of <a href="www.ivotism.com">www.ivotism.com</a>
.
function linkify($inputText) {
//URLs starting with http://, https://, or ftp://
$replacePattern1 = '/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i';
$replacedText .= preg_replace($replacePattern1, '<a href="$1" target="_blank">$1</a>', $inputText);
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
$replacePattern2 = '/(^|[^\/])(www\.[\S]+(\b|$))/i';
$replacedText .= preg_replace($replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>', $inputText);
//Change email addresses to mailto:: links.
$replacePattern3 = '/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/i';
$replacedText .= preg_replace($replacePattern3, '<a href="mailto:$1">$1</a>', $inputText);
return $replacedText;
}
I run the code as shown below:
$ab = "<br>1 http://www.ivotism.com <br>2 https://www.ivotism.com/hom.php?u=kira&id=2 <br>3 ftp://www.ivotism.com <br>4 w www.ivotism.com <br>5 info@ivotism.com"; echo
linkify($ab);
This is the result I got:
- 1 http://www.ivotism.com
- 2 https://www.ivotism.com/hom.php?u=kira&id=2
- 3 ftp://www.ivotism.com
- 4 www.ivotism.com
- 5 info@ivotism.com
- 1 http://www.ivotism.com
- 2 https://www.ivotism.com/hom.php?u=kira&id=2
- 3 ftp://www.ivotism.com
- 4 www.ivotism.com
- 5 info@ivotism.com
- 1 http://www.ivotism.com
- 2 https://www.ivotism.com/hom.php?u=kira&id=2
- 3 ftp://www.ivotism.com
- 4 www.ivotism.com
- 5 info@ivotism.com