0

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 [email protected]"; echo 
linkify($ab);

This is the result I got:

  1. 1 http://www.ivotism.com
  2. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  3. 3 ftp://www.ivotism.com
  4. 4 www.ivotism.com
  5. 5 [email protected]
  6. 1 http://www.ivotism.com
  7. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  8. 3 ftp://www.ivotism.com
  9. 4 www.ivotism.com
  10. 5 [email protected]
  11. 1 http://www.ivotism.com
  12. 2 https://www.ivotism.com/hom.php?u=kira&id=2
  13. 3 ftp://www.ivotism.com
  14. 4 www.ivotism.com
  15. 5 [email protected]
2
  • 2
    You're using $inputText as the input each time. You need to use the result of the previous replacement. Commented Dec 14, 2014 at 13:30
  • You mean i should use $replaceText in pattern 2 and 3? Commented Dec 14, 2014 at 13:40

1 Answer 1

1

You can perform all the replacements in a single call to preg_replace, by putting the regular expressions and replacements into arrays.

$resultText = preg_replace(array($replacePattern1, $replacePattern2, $replacePattern3),
                           array($replaceSub1, $replaceSub2, $replaceSub3),
                           $inputText);
Sign up to request clarification or add additional context in comments.

1 Comment

Thats great, and there is no having problem with function redeclaration. its ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.