dongyue3795 2018-12-06 18:46
浏览 32
已采纳

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

I found a working code online for auto-detecting url's from plain text. The problem I am having is creating an array to display multiple URL's if the user inputs multiple URL's into the plain text... I.E When they post a Post or Comment on my website, and lets say they have the following 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/.

With the code I have now... It will only display the first link and output the rest as plain text. I have searched for the past 7 days on-line but cannot find a working answer to this.

What I am looking for is how to turn the $url[0] into an array that automatically increases the output value on the array by 1 (depending on the amount of links the user has inputted). So in this case it should display as $url[0] then next link as $url[1] and finally the third link as $url[2]. But the main key is that I dont want a set (Fixed) number of arrays to be displayed. Instead I am looking for an unlimited array output or a max of lets say 200 Limits. I say 200 because the use in which they will be used will also be for a SEE ALSO type reference links.

My Code: (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;
        }
      ?>

Any help would be appreciated.

This is what the Output gives me with this code: (Snippet from my website)

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

As you can see from the Second Line, that the text I had input as 2 separate links are being displayed as 2 links with them both being identical to the first link that was placed in the Textfield. I am looking for them to be displayed as their own unique links and not a clone of the first link. Any help here would be greatly appreciated. Thank you.

  • 写回答

2条回答 默认 最新

  • dqotv26286 2018-12-06 19:08
    关注

    I see the problem now.

    Use preg_match_all to match all links and loop them.
    In the loop you do a simple str_replace to replace the link with the html anchor.

    $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条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?