dongrao1862 2014-08-19 19:02
浏览 25
已采纳

PHP中的正则表达式:替换字符串之间的文本

Okay I have made some progress on a problem I am solving, but need some help with a small glitch.

I need to remove all characters from the filenames in the specific path images/prices/ BEFORE the first digit, except for where there is from_, in which case remove all characters from the filename BEFORE from_.

Examples:

BEFORE                                AFTER
images/prices/abcde40.gif           > images/prices/40.gif
images/prices/UgfVe5559.gif         > images/prices/5559.gif
images/prices/wedsxcdfrom_88457.gif > images/prices/from_88457.gif

What I've done:

$pattern = '%images/(.+?)/([^0-9]+?)(from_|)([0-9]+?)\.gif%';
$replace = 'images/\\1/\\3\\4.gif';
$string = "AAA images/prices/abcde40.gif BBB images/prices/wedsxcdfrom_88457.gif CCC images/prices/UgfVe5559.gif DDD";
$newstring = str_ireplace('from_','733694521548',$string);
while(preg_match($pattern,$newstring)){
    $newstring=preg_replace($pattern,$replace,$newstring);
}
$newstring=str_ireplace('733694521548','from_',$newstring);
echo "Original:
$string

New:
$newstring";

My expected output is:

AAA images/prices/40.gif BBB images/prices/from_88457.gif CCC images/prices/5559.gif DDD"

But instead I am getting:

AAA images/prices/40.gif BBB images/from_88457.gif CCC images/5559.gif DDD

The prices/ part of the path is missing from the last two paths.

Note that the AAA, BBB etc. portions are just placeholders. In reality the paths are scattered all across a raw HTML file parsed into a string, so we cannot rely on any pattern in between occurrences of the text to be replaced.

Also, I know the method I am using of substituting from_ is hacky, but this is purely for a local file operation and not for a production server, so I am okay with it. However if there is a better way, I am all ears!

Thanks for any assistance.

  • 写回答

4条回答 默认 最新

  • douping5015 2014-08-19 19:15
    关注

    You can use lookaround assertions:

    preg_replace('~(?<=/)(?:([a-z]+)(?=\d+\.gif)|(\w+)(?=from_))~i', '', $value);
    

    Explanation:

    (?<=/)          # If preceded by a '/':
    (?:             # Begin group
     ([a-z]+)       #   Match alphabets from a-z, one or more times
     (?=\d+\.gif)   #   If followed followed by digit(s) and '.gif'
     |              #   OR
     (\w+)          #   Match word characters, one or more times
     (?=from_)      #   If followed by 'from_'
    )               # End group
    

    Visualization:

    Image from debuggex

    Code:

    $pattern = '~(?<=/)(?:([a-z]+)(?=\d+\.gif)|(\w+)(?=from_))~i';
    echo preg_replace($pattern, '', $string);
    

    Demo

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

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000