0

PHP 正则表达式提取URL

karan created at6 years ago view count: 2705

怎样从一段文本中提取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"/>');
6 years ago 回复

Recent search keywords