-1

I have a text from user input. In the text includes all kind emails and links start with "http/https/www". For example: I have a text like below.

Lorem ipsum dolor email@foo.com sit amet https://foo.com/path?query=string 
Lorem ipsum foo.com www.foo.com Lorem ipsum dolor

Exected output would be.

Lorem ipsum dolor <a href="mailto:email@foo.com>email@foo.com</a> sit amet <a href="https://foo.com/path?query=string">https://foo.com/path?query=string</a>
Lorem ipsum <a href="foo.com">foo.com</a> <a href="www.foo.com">www.foo.com</a> Lorem ipsum dolor
Hoang Trung
  • 1,979
  • 1
  • 21
  • 33
  • It would be greate if I can use php to solve the problem, otherwise I'll use [this javascript library](https://alexcorvi.github.io/anchorme.js/) as a last resort – Hoang Trung Jun 02 '17 at 11:21
  • I've edited the solution :) – Sreetam Das Jun 02 '17 at 12:06
  • Possible duplicate of [Convert plain text URLs into HTML hyperlinks in PHP](https://stackoverflow.com/questions/1960461/convert-plain-text-urls-into-html-hyperlinks-in-php) –  Jun 02 '17 at 12:51

1 Answers1

1

Try this code, please refer this demo

<?php
$linkArray = ['Lorem ipsum dolor sit amet','email@gmail.com','google.com','https://www.google.com'];
foreach($linkArray as $link){
    if(preg_match('/((https:\/\/)?([A-z]+@)?([A-z]+)?\.([A-z]+)?)/',$link,$match)){
        if(preg_match('/(@)/',$link,$m)){
            echo '<a href="mailto:'.$match[1].'">'.$match[1].'</a><br>';    
        }else{
            echo '<a href="'.$match[1].'">'.$match[1].'</a><br>';
        }
    }else{
        echo $link.'<br>';
    }
}
?>
Bhaumik Pandhi
  • 2,655
  • 2
  • 21
  • 38