在纯文本中检测到的多个URL的自动递增数组

时间:2018-12-06 18:46:53

标签: php

我在线找到了一个工作代码,用于自动检测纯文本中的url。我遇到的问题是创建要显示的数组 如果用户在纯文本中输入了多个URL,则为多个URL ...即当他们在我的网站上发布帖子或评论时,以及 可以说他们有以下文字:

I found some really good articles on such and such topic. Here are a few links to check out: 
http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and 
http://example-site.com/today-is-a-beautiful-day/.

使用我现在拥有的代码...它将仅显示第一个链接并输出 以纯文本形式显示。我已经在线搜索了过去7天,但是找不到有效的答案。

我要寻找的是如何将$url[0]变成一个数组,该数组会自动将数组的输出值增加1 (取决于用户输入的链接数量)。因此,在这种情况下,它应显示为$url[0],然后下一个链接显示为$url[1] 最后,第三个链接为$url[2]。但是主要的关键是我不希望显示一组固定的数组。代替 我正在寻找无限的数组输出或最多可以说200个限制。我说200是因为将使用它们 也适用于SEE ALSO类型的引用链接。

我的代码:(PHP)

      <?php
        // The Regular Expression filter
        $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
        // The Text you want to filter for urls
        $text = $faq_seealso;  // The variable $faq_seealso refers to my MySQL data fetch query.
                                                         // I will provide it if needed.
        // Check if there is a url in the text
        if(preg_match($reg_exUrl, $text, $url)) {

            // make the urls hyper links
            echo preg_replace($reg_exUrl, "<a href={$url[0]}>{$url[0]}</a> ", $text); // <--- Part in question

        } else {
            // if no urls in the text just return the text
            echo $text;
        }
      ?>

任何帮助将不胜感激。

这是输出为我提供的以下代码:(来自我的网站的摘录)

1. https://www.pokemongosrf.ca https://www.pokemongosrf.ca
1. https://www.pokemongosrf.ca http://kambel.ca

从第二行可以看到,我作为2个单独的链接输入的文本将显示为2个链接,它们都与放置在“文本字段”中的第一个链接相同。我希望它们显示为自己的唯一链接,而不是第一个链接的副本。在这里的任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

我现在看到了问题。

使用preg_match_all匹配所有链接并使它们循环。
在循环中,您执行了一个简单的str_replace,将链接替换为html锚。

$text ="I found some really good articles on such and such topic. Here are a few links to check out: http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and http://example-site.com/today-is-a-beautiful-day/. ";
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls     

// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $text, $url)) {
    foreach($url[0] as $link){
        $text = str_replace($link, "<a href={$link}>{$link}</a> ", $text); // <--- Part in question
    }
    echo $text;
} else {
    // if no urls in the text just return the text
    echo $text;
}
//I found some really good articles on such and such topic. Here are a few links to check out: <a href=http://www.example.com/hOSDHOUA>http://www.example.com/hOSDHOUA</a>  and <a href=https://www.mywebsite.com/h/yIFeelLowIfImHigh>https://www.mywebsite.com/h/yIFeelLowIfImHigh</a>  and <a href=http://example-site.com/today-is-a-beautiful-day/.>http://example-site.com/today-is-a-beautiful-day/.</a>  

https://3v4l.org/W4ifF

答案 1 :(得分:1)

您应该在替换字符串中使用$0来替换正则表达式的相应匹配项。使用$url[0]只会返回preg_match()的第一个匹配项。

echo preg_replace($reg_exUrl, "<a href=$0>$0</a> ", $text);

DEMO