Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Preg Replace - Fatal编程技术网

Php 使链接在文本块中可单击的最佳方法

Php 使链接在文本块中可单击的最佳方法,php,regex,preg-replace,Php,Regex,Preg Replace,我想: 成为: 试着这样做: function make_links_clickable($text) { return preg_replace ('/http:\/\/[^\s]+/i', "<a href=\"${0}\">${0}</a>", $text); } $result = make_links_clickable($text); 功能使链接可点击($text) { 返回preg\u replace('/http:\/\/[^\s]+/i','

我想:

成为:


试着这样做:

function make_links_clickable($text)
{
   return preg_replace ('/http:\/\/[^\s]+/i', "<a href=\"${0}\">${0}</a>", $text);
} 

$result = make_links_clickable($text);
功能使链接可点击($text)
{
返回preg\u replace('/http:\/\/[^\s]+/i','',$text);
} 
$result=使链接可点击($text);
使用此选项(适用于ftp、http、ftps和https方案):

功能使链接可点击($text){
返回preg_replace(“!((f|ht)tp(s):/)[-a-zA-Zа-a-a-a-a-h()0-9:%:%。~#?&;/=]+!i',“,$text”);
}

受Akarun回答的启发,我想出了一个tis函数来处理所有只从
www.开始的协议和链接。

function make_links($text, $class='', $target='_blank'){
    return preg_replace('!((http\:\/\/|ftp\:\/\/|https\:\/\/)|www\.)([-a-zA-Zа-яА-Я0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?!ism', 
    '<a class="'.$class.'" href="//$3" target="'.$target.'">$1$3</a>', 
    $text);
}
函数生成链接($text,$class='',$target=''\u blank')){
返回preg\u replace('!((http:\/\/\\\/\\\\/\\/\\/\\\/\\\\/\\\\/\\\\\\\/\\\\\\\\\\\\\\\\\\\\/\\\\\\\.\:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
'', 
$text);
}

此函数具有可选参数,用于将类名添加到链接上,还具有可选的链接目标,因此它们将在新窗口/选项卡上打开。。。默认情况下,该参数打开指向新窗口/选项卡的链接,但如果您不想这样做,则可以更改默认值,或在调用函数时更改值。

同样受Akarun答案的启发,以下函数将仅链接尚未链接的文本。添加的功能是检查目标字符串中是否不存在包含捕获的文本链接的链接:

function make_links_from_http($content) {   
    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) {
        if (!preg_match('!<a(.*)'.$link.'(.*)/a>!i', $content))
        {
            $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
        }
    }

    return $content;
} 
函数从http($content){
//文本外链接
preg|u matchúu all('!((f|ht)tp(s):/)[-a-zA-Zа-a-a-a-a-Я()0-9:%:%|+.~#?&;/=]+!i',$content,$matches);
foreach($key=>$link与[0]匹配){
如果(!preg_match(“!”,$content);
}
}
返回$content;
} 
通过测试,我注意到上述函数在第5行出现故障。执行此任务的“messier”函数如下所示:

function make_links_from_http($content) 
{
    // The link list
    $links = array();

    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) 
    {
        $links[$link] = $link;
    }

    // Get existing
    preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $content, $matches);
    foreach ($matches[2] as $key=>$value)
    {
        if (isset($links[$value]))
        {
            unset($links[$value]);
        }
    }

    // Replace in content
    foreach ($links as $key=>$link)
    {
        $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
    }

    return $content;
} 
函数从http($content)生成链接
{
//链接列表
$links=array();
//文本外链接
preg|u matchúu all('!((f|ht)tp(s):/)[-a-zA-Zа-a-a-a-a-Я()0-9:%:%|+.~#?&;/=]+!i',$content,$matches);
foreach($key=>$link与[0]匹配)
{
$links[$link]=$link;
}
//生存
preg_match_all('/',$content);
}
返回$content;
} 
对于新代码,我使用了以下位置的教程:
函数makeClickableLinks($text)
{
$text=html\u实体\u解码($text);
$text=”“.$text;

$text=preg\u replace(“/(^ |[\n])([\w]*?)([\w]*?:\/\/[\w]+[^\,\”\n\r\t Akarun和Lawrence Cherone建议的链接中提供的解决方案更完整,因为他们检查url以获取有效的url,还检测https和ftp链接,在显示链接之前对链接进行url解码。我的解决方案更脏,但它应该适用于简单的任务。这个答案缺少教育性解释。问题是什么
放在regexp开头和结尾的字符?regex中的非ASCII字符是什么?(“Я”和“Я”)?@NeilHillman:包括它们是为了支持西里尔文域名(А和Я都是西里尔文)•这对不以http:///开头的链接不起作用。我建议添加一个str_replace查找www.并添加http://infront。然后,我会添加另一个str_replace checkign以替换为双htt p://htt p://的str_replace checkignone@Akarun如果链接是这样的呢?
(https://www.sample.com/request).
该函数将包括“)”和“.”最后,看起来像是这样的
这些接二连三的替换没有得到任何解释。这个答案是不完整的,任何使用这个答案的人都可能是盲目地复制粘贴。这个答案应该编辑得更慷慨/更有教育意义。应该只有一个
preg_replace()
call;应该有一个模式数组和一个替换数组。当您需要执行嵌套在
preg\u
调用中的
preg\u
调用时,这通常表示第一个正则表达式模式没有很好地构建。当您匹配匹配事件只是为了迭代和替换匹配的内容时,这通常表示表示您应该使用
preg\u replace\u callback()
。此模式由于模式中不必要的转义而过度膨胀。问问自己,如果没有
(任何字符“点”),为什么要使用
s
模式修饰符在模式中。然后问问自己,如果没有
^
$
(锚定),为什么要使用
m
模式修饰符在模式中。
http\:\/\/\/\\;https\:\/\/
是不必要的冗长。
a-zA-Zаa-Яa-Яa-Яa-Я
是不必要的冗长。我不建议任何人使用这个片段,即使它提供了预期的结果,因为它不是在教授最佳实践。
function make_links_clickable($text){
    return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text);
}
function make_links($text, $class='', $target='_blank'){
    return preg_replace('!((http\:\/\/|ftp\:\/\/|https\:\/\/)|www\.)([-a-zA-Zа-яА-Я0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?!ism', 
    '<a class="'.$class.'" href="//$3" target="'.$target.'">$1$3</a>', 
    $text);
}
function make_links_from_http($content) {   
    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) {
        if (!preg_match('!<a(.*)'.$link.'(.*)/a>!i', $content))
        {
            $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
        }
    }

    return $content;
} 
function make_links_from_http($content) 
{
    // The link list
    $links = array();

    // Links out of text links
    preg_match_all('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', $content, $matches);
    foreach ($matches[0] as $key=>$link) 
    {
        $links[$link] = $link;
    }

    // Get existing
    preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $content, $matches);
    foreach ($matches[2] as $key=>$value)
    {
        if (isset($links[$value]))
        {
            unset($links[$value]);
        }
    }

    // Replace in content
    foreach ($links as $key=>$link)
    {
        $content = str_replace($link, '<a href="'.$link.'" target="_blank">'.$link.'</a>', $content);
    }

    return $content;
} 
function makeClickableLinks($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text= preg_replace("/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $text);  
$text= preg_replace("/(^|[\n ])([\w]*?)((www|wap)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $text);
$text= preg_replace("/(^|[\n ])([\w]*?)((ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"$4://$3\" >$3</a>", $text);  
$text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text);  
$text= preg_replace("/(^|[\n ])(mailto:[a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"$2@$3\">$2@$3</a>", $text);  
$text= preg_replace("/(^|[\n ])(skype:[^ \,\"\t\n\r<]*)/i", "$1<a href=\"$2\">$2</a>", $text);  
        return $text;
}