Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在页面中查找链接并通过自定义函数运行它_Php_Regex_Url - Fatal编程技术网

Php 在页面中查找链接并通过自定义函数运行它

Php 在页面中查找链接并通过自定义函数运行它,php,regex,url,Php,Regex,Url,这样做的目的是获取url并将其返回。稍后将致力于将其转换为完整的url。所以像t.co一样,他们将看到完整的url function shorturl2full($url) { echo 'URL IS: ' . $url; return "FULLLINK"; } $text=preg\u replace(“/(^ |[\n])([\w]*?)((ht | f)tp(s)?:\/\/\/[\w]+[^\,\”\n\r\t在一个例子中,我展示了一个名为使可点击的函数,它有一个可

这样做的目的是获取url并将其返回。稍后将致力于将其转换为完整的url。所以像
t.co
一样,他们将看到完整的url

function shorturl2full($url)
{
    echo 'URL IS: ' . $url;
    return "FULLLINK";
}
$text=preg\u replace(“/(^ |[\n])([\w]*?)((ht | f)tp(s)?:\/\/\/[\w]+[^\,\”\n\r\t在一个例子中,我展示了一个名为
使可点击的
函数,它有一个可选的回调参数,如果设置了该参数,它将应用于每个URI:

$text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" target=\"_blank\">$2@$3</a>", $text);  
        return($text);  
}

也许它有帮助,或者至少提供了一些想法。

您可以使用preg\u replace\u回调来代替preg\u replace

function link\u it($text)
{
$text=preg\u replace\u回调(“/(^ |[\n])([\w]*?)((ht | f)tp(s)?:\/\/\/[\w]+[^\,\”\n\r\t
$text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" target=\"_blank\">" .  shorturl2full("$3") . "</a>", $text);  
        $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" target=\"_blank\">$2@$3</a>", $text);  
        return($text);  
}
make_clickable($text, 'shorturl2full');
function link_it($text)
{
    $text= preg_replace_callback("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is",  'shorturl2full', $text);  
    $text= preg_replace_callback("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is",  'shorturl2full', $text);  
    $text= preg_replace_callback("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i",  'shorturl2full', $text);  
    return($text);  
}

function shorturl2full($url)
{
    $fullLink = 'FULLLINK';
    // $url[0] is the complete match
    //... you code to find the full link
    return '<a href="' . $url[0] . '">' . $fullLink . '</a>';
}