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条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作