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 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答