Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

regex - PHP - Function To Find Links In Text

I have got a function that converts strings like 'www.example.com' and 'http://example.com' in hyperlinks. It also deals with subdomains e.g. 'http://sub.example.com'.

But it fails with this one - http://www.example.com' and outputs the following

<a href="http://<a href="http://www.chemica.ru">www.chemica.ru</a>">http://<a href="http://www.chemica.ru">www.chemica.ru</a></a>

Please, can anyone help? The problem is that both 'http://' and 'www.' are together and both have different ways of converting.

function makeLinks($text){ 
 $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', '<a href="\1">\1</a>', $text); 
 $text = eregi_replace('(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', '<a href="http://\1">\1</a>', $text);
 $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})', '<a href="mailto:\1">\1</a>', $text); 
 return $text; 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just write to your view's page (you dont need any library or helper function) :

$text = "Good Site is http://masalahkita.com";
$link = preg_replace("/([w]+://[w-?&;#~=./@]+[w/])/i","<a target="_blank" href="$1">$1</a>", $text);

echo $link;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...