Php preg_替换取出额外的字符串
我有一个preg_replace,取出了一根不应该移除的绳子的一部分。 它应该寻找:图片\客户/39/结构/第二方/ 并将其替换为:images\u client/39/structure/xxx/ 它确实做到了这一点,但它也删除了苹果logo.jpg之前的图像/部分图像Php preg_替换取出额外的字符串,php,preg-replace,Php,Preg Replace,我有一个preg_replace,取出了一根不应该移除的绳子的一部分。 它应该寻找:图片\客户/39/结构/第二方/ 并将其替换为:images\u client/39/structure/xxx/ 它确实做到了这一点,但它也删除了苹果logo.jpg之前的图像/部分图像 <?php echo '<br>-------- preg_replace on a css string with slashes ------<br>'; $string=
<?php
echo '<br>-------- preg_replace on a css string with slashes ------<br>';
$string='/images_client/39/structure/party-2/images/apple_logo.jpg';
echo 'Before: '.$string.'<br>';
print preg_replace("/images_client\/39\/structure\/(\S+)\//", "images_client/39/structure/xxx/", $string) . "\n";
?>
试试这个:
preg_replace('#images_client/39/structure/[^/]+/#s', 'images_client/39/structure/xxx/');
请尝试[^/]而不是\S:
请在处理包含正斜杠的图案时,选择一个不同的图案分隔符,您可以选择任何您想要的:所有这些\/使眼睛流血。你也可以考虑接受一些答案。
/images_client\/39\/structure\/([^\/]+)\//