0

PHP 正则表达式提取URL

karan created at5 years ago view count: 2664

怎样从一段文本中提取URL

report
回复
0
<?php

function getUrl($string)
{
    $url = '(https?|ftp|file):\/\/[-A-Za-z0-9+&@#\/%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]';
    if (preg_match("/(?P<url>$url)/i", $string, $match)) {
        return ($match['url']);
    }
    return false;
}
echo getUrl('  <link rel="canonical" href="http://www.infoq.com/cn"/>');
5 years ago 回复

Recent search keywords