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

报告相同问题?

悬赏问题

  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?