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

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>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 参考 - 这个正则表达式意味着什么? 1回答 \ r

    好的 - 这让我感到震惊了好几天。 我已尝试使用负前瞻的正则表达式,但无济于事。

    基本上,在PHP中,我需要解析会话线程并提取可能自身发生的最后一次出现的http链接, 或连续2个或更多的组。 因此,在示例1中,它应该返回最后一个链接,但是在示例2中,它应该返回最后3个链接。

    我不需要使用单个正则表达式实现此目的, 但我不确定还有其他尝试方法。 任何帮助将不胜感激!!

    示例1:

    Lorem ipsum dolor sit amet,consectetur adipiscing elit。

    http://sample.com/12345.png

    在pharetra elementum dui vel pretium中。 Quisque rutrum maurise turpis hendrerit facilisis。 Sed ultrices imperdiet ornare。

    http://sample.com/13578。 png


    示例2:

    Lorem ipsum dolor sit amet,consectetur adipiscing elit。

    http://sample.com/12345.png

    在pharetra elementum dui vel pretium中。 Quisque rutrum maurise turpis hendrerit facilisis。 Sed ultrices imperdiet ornare。

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


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题