duandian4501 2018-09-09 18:05
浏览 527
已采纳

PHP,正则表达式最后出现一个(或多个)字符串[重复]

This question already has an answer here:

Okay - this has boggled me for days. I've tried regex with negative lookahead, but to no avail.

Basically, in PHP, I need to parse conversation threads and extract the LAST occurrence of http links that can occur by itself, or in a consecutive group of 2 or more. So, in example 1, it should return the last link, but in example 2, it should return the last 3 links.

I don’t need to achieve this with a single regex, but I’m not sure what other approaches to try. Any help would be appreciated!!

EXAMPLE 1:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

http://sample.com/12345.png

In pharetra elementum dui vel pretium. Quisque rutrum mauris vitae turpis hendrerit facilisis. Sed ultrices imperdiet ornare.

http://sample.com/13578.png


EXAMPLE 2:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

http://sample.com/12345.png

In pharetra elementum dui vel pretium. Quisque rutrum mauris vitae turpis hendrerit facilisis. Sed ultrices imperdiet ornare.

http://sample.com/24689.png
http://sample.com/13578.png
http://sample.com/98761.png


</div>
  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2018-09-09 18:27
    关注

    1) Split your Text on the delimiter \s.

    $resultArray = preg_split("@\s@", $conversation)
    

    on example:

    $conversation = "Hallo, http://1.de text http://2.de
    http://3.de Hello";
    

    (This will produce something like this as intermediate result:)

    Array
    (
        [0] => Hallo,
        [1] => http://1.de
        [2] => text
        [3] => http://2.de
        [4] => 
        [5] => http://3.de
        [6] => Hello
    )
    

    2.) Finally, reverse iterate over the result array. Start "matching", if the result starts with "http://" - stop matching if you encounter anything else, Ignore Empty lines as well as lines with whitespaces only.:

    $conversation = "Hallo, http://1.de text http://2.de
    http://3.de Hello";
    $resultArray = preg_split("@\s@", $conversation);
    $result = array();
    
    $matching = false;
    for ($i = count($resultArray)-1; $i >= 0; $i--){
        if (preg_match("@http:\/\/@", $resultArray[$i])){
          $matching=true;
          $result[] = $resultArray[$i];  
        } else if (preg_match("@^\s*$@", $resultArray[$i])){
           //ignore this bad boy
        }else{
            if ($matching){
                break;
            }
        }
    }
    
    echo "<pre>";
    print_r(array_reverse($result));
    echo "</pre>";
    

    yields:

    Array
    (
        [0] => http://2.de
        [1] => http://3.de
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)