douba8048 2014-02-20 04:37
浏览 57
已采纳

从php中的任何字符串中提取所有url / domains

I use php and get the following text string from a textbox.

This is a string I have:

header1            header2             edit
b-1246431          12.01.13            1246431  |  blog.domain.net            1232,00 ‌‌
details
b-1312231          12.01.13            1246431  |  blog.domain.co.uk          12312,00
b-2344311          12.01.13            1246431  |  www.domain.com/             9129,00 ‌‌
b-2344322          12.01.13            1246431  |  http://abc.de              1332,00 ‌‌
b-2344322          13.01.13            1246431  |  www.cdf.de/                 21140,00             ‌‌edit
b-1233422          06.01.13            1246431  |  www.dto.de/site1      21110,00
b-1233542          06.01.13            1246431  |  www.ghj.ca/site2.html      28110,00             ‌‌             edit
b-1231242          06.01.13            1246431  |  www.another.de            2101,00             ‌‌
b-1231231          04.01.13            1246431  |  onlyme.info/  

I want this output:

blog.domain.net
blog.domain.co.uk
www.domain.com/
http://abc.de
www.cdf.de/
www.dto.de/site1
www.ghj.ca/site2.html
www.another.de
onlyme.info/  

The string will change. I always need only the urls extracted. The problem might be: sometimes urls start with www, http, or dont even have both. Still they should be seen as urls.

I already looked up these posts: extracting one or more urls from a string in php http://daringfireball.net/2010/07/improved_regex_for_matching_urls

... but nothing worked for my textstring...

  • 写回答

2条回答 默认 最新

  • dongzhizhai4070 2014-02-20 04:53
    关注

    Try it with a regular expression:

    <?php
    $input = "header1            header2             edit
    b-1246431          12.01.13            1246431  |  blog.domain.net            1232,00 ‌‌
    details
    b-1312231          12.01.13            1246431  |  blog.domain.co.uk          12312,00
    b-2344311          12.01.13            1246431  |  www.domain.com/             9129,00 ‌‌
    b-2344322          12.01.13            1246431  |  http://abc.de              1332,00 ‌‌
    b-2344322          13.01.13            1246431  |  www.cdf.de/                 21140,00             ‌‌edit
    b-1233422          06.01.13            1246431  |  www.dto.de/site1      21110,00
    b-1233542          06.01.13            1246431  |  www.ghj.ca/site2.html      28110,00             ‌‌             edit
    b-1231242          06.01.13            1246431  |  www.another.de            2101,00             ‌‌
    b-1231231          04.01.13            1246431  |  onlyme.info/";
    
    preg_match_all('#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si', $input, $result);
    
    foreach ($result[0] as $url)
    {
        echo $url . "<br />
    ";
    }
    

    Or see my PHPFiddle here: PHPFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。