Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中的正则表达式:在字符串之间替换文本_Php_Regex - Fatal编程技术网

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

PHP中的正则表达式:在字符串之间替换文本,php,regex,Php,Regex,好的,我在解决一个问题上取得了一些进展,但有一个小问题需要一些帮助 我需要删除特定路径images/prices/中第一个数字之前的文件名中的所有字符,除了\u中有的地方,在这种情况下,从\u中删除之前的文件名中的所有字符 示例: BEFORE AFTER images/prices/abcde40.gif > images/prices/40.gif images/prices/UgfVe5559.gif

好的,我在解决一个问题上取得了一些进展,但有一个小问题需要一些帮助

我需要删除特定路径
images/prices/
中第一个数字之前的文件名中的所有字符,除了\u中有
的地方,在这种情况下,从\u
中删除
之前的文件名中的所有字符

示例:

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
我所做的:

$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:\n$string\n\nNew:\n$newstring";
我的预期产出是:

AAA images/prices/40.gif BBB images/prices/from_88457.gif CCC images/prices/5559.gif DDD"
但我得到的却是:

AAA images/prices/40.gif BBB images/from_88457.gif CCC images/5559.gif DDD
最后两条路径缺少路径的
prices/
部分

请注意,
AAA
BBB
等部分只是占位符。实际上,路径分散在解析为字符串的原始HTML文件中,因此我们不能依赖于要替换的文本出现之间的任何模式

另外,我知道我正在使用的从
替换
的方法是有漏洞的,但这纯粹是为了本地文件操作,而不是为了生产服务器,所以我可以接受。不过,如果有更好的办法,我洗耳恭听

谢谢你的帮助

$arr = array(
    'images/prices/abcde40.gif',
    'images/prices/UgfVe5559.gif',
    'images/prices/wedsxcdfrom_88457.gif'
);

foreach($arr as $str){
    echo preg_replace('#images/prices/.*?((from_|\d).*)#i','images/prices/$1',$str);
}

编辑:
您可以使用此正则表达式进行替换:

^(images/prices/)\D*?(from_)?(\d+\..+)$
$1$2$3
并使用此表达式替换:

^(images/prices/)\D*?(from_)?(\d+\..+)$
$1$2$3
代码:

$re = '~^(images/prices/)\D*?(from_)?(\d+\..+)$~m'; 
$str = "images/prices/abcde40.gif\nimages/prices/UgfVe5559.gif\nimages/prices/wedsxcdfrom_88457.gif";     
$result = preg_replace($re, '$1$2$3', $str);
$pattern = '~(?<=/)(?:([a-z]+)(?=\d+\.gif)|(\w+)(?=from_))~i';
echo preg_replace($pattern, '', $string);

您也可以尝试使用环视。只需替换为空白字符串

(?<=^images\/prices\/).*?(?=(from_)?\d+\.gif$)

您可以使用lookaround断言:

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

preg_replace(“~”(?谢谢,但是如果我用空格替换换行符,代码将失败,并且只显示最后一项。我需要替换长字符串中的每个匹配实例。有什么想法吗?谢谢。我对正则表达式语法非常不熟悉,因此可能缺少一些内容,但我设置了
$re=”(?谢谢,但是如果我用空格替换换行符,代码会失败。只替换第一项,其余两项不会。我需要替换长字符串中的每个匹配实例。有什么想法吗?你检查过我的链接演示了吗?所有图像都被替换了。你可能缺少
m
标志。我对正则表达式非常不好!我尝试在换行符替换为空格的情况下运行代码,结果如下:-最后两个换行符未被替换。在这种情况下,正则表达式将为:
$re='~(images/prices/)\D*?(from?)(\D+\..+)~';
谢谢,但是我不能在这里使用干净漂亮的数组-问题是我需要替换来处理长字符串中的所有匹配实例。@RC您仍然可以使用相同的正则表达式。我更新了答案。谢谢,但是我不能在这里使用干净漂亮的数组-问题是我需要替换ent来处理长字符串中的所有匹配实例。@RC:没关系。它也应该处理普通字符串。。是的!很抱歉,我看到了太多使用长字符串失败的数组输入的解决方案,所以我没有对其进行测试。非常感谢!还感谢您在映射语法方面所做的努力-非常感谢!抱歉,只是r我想问一下:如何修改您的模式,使其只替换
图像/价格/
路径中的文件名,而忽略其他路径中的所有其他类似文件名?@RC:只需将lookback更改为
(?)?
$pattern = '~(?<=/)(?:([a-z]+)(?=\d+\.gif)|(\w+)(?=from_))~i';
echo preg_replace($pattern, '', $string);