douhan9619 2018-11-09 10:42
浏览 67
已采纳

删除PHP字符串中最后一次出现的非连续,重复的单词/阶段

OK, I've been trying to figure this one out for a while, but I can't seem to get it right. I need to delete the last occurrence of a non-consecutive, duplicate word/phrase from the END of a string. For example, I want

Love in My Antonia Love in

and

Love in My Antonia Love

to become

Love in My Antonia

I have tried countless patterns without success. The closest I have gotten to success is this:

 preg_replace('/\b(\w{2,})\b(?=.*?\\1)\W*/', ''

That deletes the first occurrence (rather than the last), rendering:

 in My Antonia Love (ORIGINAL: "Love in My Antonia Love")

and

 My Antonia Love in (ORIGINAL: "Love in My Antonia Love in")

Please help! :)



UPDATE (Nov. 9, 2:00 PM, PST): I should have clarified that—if at all possible—I would like the solution to remain in the simple, 1-line, compact format that I showed in my example:

preg_replace('/\b(\w{2,})\b(?=.*?\\1)\W*/', ''

My example already works almost perfectly, except that it deletes the first match instead of the last. I was hoping that someone could modestly manipulate my existing code so that it deletes the last match (at the end of the string) instead of the first occurrence. Is that more complicated than I thought?



Previously, I came up with a version that finds two consecutive, duplicate words/phrases anywhere in a string and replaces them with one:

preg_replace('~\b([\S \w]{3,})\K\b(?:\s*\1)+~', '', 

This makes "pizza pizza" become "pizza" and "I walked to the store I walked to the store" become "I walked to the store." That's great, and I have already incorporated that solution. Now, secondarily, I also need "Pizza is the best pizza" to become "Pizza is the best." Similarly, "Sheep dogs are awesome pets dogs are" should become "Sheep dogs are awesome pets." So, basically, it does not matter where in the string the first occurrence appears; all that matters is that the occurrence at the END OF THE STRING gets deleted. I hope this brings more clarity.

  • 写回答

3条回答 默认 最新

  • douzhan5058 2018-11-09 22:59
    关注

    Here you go:

    $s = preg_replace('/^\b([\w ]+)(.*?)\b(\1)$/i', '\\1\\2', $s);
    

    Test:

    $s = "Love in My Antonia Love in";
    $s1 = "Love in My Antonia Love";
    $s2 = "Love in My Antonia Love Not On End";
    
    echo "Original:
    $s
    ";
    echo preg_replace('/^\b([\w ]+)(.*?)\b(\1)$/i', '\\1\\2', $s);
    echo "
    ";
    echo "Original:
    $s1
    ";
    echo preg_replace('/^\b([\w ]+)(.*?)\b(\1)$/i', '\\1\\2', $s1);
    echo "
    ";
    echo "Original:
    $s2
    ";
    echo preg_replace('/^\b([\w ]+)(.*?)\b(\1)$/i', '\\1\\2', $s2);
    

    Output:

    ZC-MGMT-04:~ jv$ php -q c.php
    Original:
    Love in My Antonia Love in
    Love in My Antonia
    Original:
    Love in My Antonia Love
    Love in My Antonia
    Original:
    Love in My Antonia Love Not On End
    Love in My Antonia Love Not On End
    

    ====

    UPDATE:

    Jason suggests slight update to address ' at the end of the word:

    preg_replace('/^\b([\w ]+)(.*?)\b\b(\1)(\'s)*\b$/i', '\\1\\2')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度