Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 preg_替换不带http、https或www的URL_Php_Arrays_Preg Replace_Tld - Fatal编程技术网

PHP preg_替换不带http、https或www的URL

PHP preg_替换不带http、https或www的URL,php,arrays,preg-replace,tld,Php,Arrays,Preg Replace,Tld,我对正则表达式之类的东西不太在行。我知道如何将www.google.com更改为链接。但是,我希望脚本获得以下字符串的链接: Hello. Have you visited [link goes here]google.com[/link goes here] today? Hello. Have you visited [link goes here]www.google.com[/link goes here] today? Hello. Have you visited [link goe

我对正则表达式之类的东西不太在行。我知道如何将www.google.com更改为链接。但是,我希望脚本获得以下字符串的链接:

Hello. Have you visited [link goes here]google.com[/link goes here] today?
Hello. Have you visited [link goes here]www.google.com[/link goes here] today?
Hello. Have you visited [link goes here]http://google.com[/link goes here] today?
Hello. Have you visited [link goes here]https://google.com[/link goes here] today?
当然,我真的希望表达式允许尽可能多的字符。但是对于第一个链接,我只能想到一个解释(我不希望人们开始写text.text,它将成为一个链接):


你们有谁知道该怎么做吗P

我希望它与Autolinker.js类似,只是在PHP中:

$template=我刚刚浏览了一个半体面的正则表达式来匹配域,并对其进行了一些调整-如果您继续查找,可能会有更好的正则表达式

<?php

$test = 'Hello. Have you visited google.com today?
Hello. Have you visited www.google.com today?
Hello. Have you visited http://google.com today?
Hello. Have you visited https://google.com today?';

$func = function ($match) {

    $text   = trim($match[0]);
    $pieces = parse_url($text);
    $scheme = array_key_exists('scheme', $pieces) ? $pieces['scheme'] : 'http';
    $host   = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
    $link   = sprintf('%s://%s', $scheme, $host);

    return sprintf('<a href="%s">%s</a>', $link, $text);
};

echo preg_replace_callback('/((http[s]?:\/\/)?(?>[a-z\-0-9]{2,}\.){1,}[a-z]{2,8})(?:\s|\/)/m', $func, $test);

请以后尝试谷歌搜索。。。在告诉我使用谷歌之前:阅读我的要求。。我希望google.com变成超链接,即使没有输入http://、https://或www。我知道如何编写代码来建立与协议的链接,但是,我想把没有协议的链接也变成链接。
$template = <<< EOF
Hello. Have you visited google.com today?
Hello. Have you visited www.google.com today?
Hello. Have you visited http://google.com today?
Hello. Have you visited https://google.com today?
EOF;

$template = preg_replace_callback('/(?=(([\w\/\/:.]+)\.(?:com|net|org|info|no|dk|se)))\b(?:(?:https?|ftp|file):\/\/|(?:www\.|ftp\.)?)
      (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*
      (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/ix','my_callback',$template);

function my_callback($matches) {

 //check it the link has the protocol if not adds it.
if (preg_match('/https?/ix', $matches[1])) {
    $link = $matches[1];
    return "<a href=\"$link\">$link</a>";
} else {
    $link = $matches[1];
    return "<a href=\"http://$link\">http://$link</a>";
}
}

echo $template;
<?php

$test = 'Hello. Have you visited google.com today?
Hello. Have you visited www.google.com today?
Hello. Have you visited http://google.com today?
Hello. Have you visited https://google.com today?';

$func = function ($match) {

    $text   = trim($match[0]);
    $pieces = parse_url($text);
    $scheme = array_key_exists('scheme', $pieces) ? $pieces['scheme'] : 'http';
    $host   = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
    $link   = sprintf('%s://%s', $scheme, $host);

    return sprintf('<a href="%s">%s</a>', $link, $text);
};

echo preg_replace_callback('/((http[s]?:\/\/)?(?>[a-z\-0-9]{2,}\.){1,}[a-z]{2,8})(?:\s|\/)/m', $func, $test);
Hello. Have you visited <a href="http://google.com">google.com</a>today?
Hello. Have you visited <a href="http://www.google.com">www.google.com</a>today?
Hello. Have you visited <a href="http://google.com">http://google.com</a>today?
Hello. Have you visited <a href="https://google.com">https://google.com</a>today?