2010-01-23 21 views
1

我有一個preg_replace取出它不應該刪除的字符串的一部分。 它應該在尋找:images_client/39/structure/party-2/ 並將其替換爲:images_client/39/structure/xxx/ 它的確如此,但它也在apple_logo之前刪除圖像/它的一部分.JPGpreg_replace取出一個額外的字符串

<?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"; 
?> 
+1

在處理含有正斜槓的圖案時,請選擇一個不同的圖案分隔符(您可以隨意選擇任何圖案):所有這些'\ /'都會使眼睛流血。你也可以考慮接受一些答案 – 2010-01-23 21:51:34

回答

0

試試這個:

preg_replace('#images_client/39/structure/[^/]+/#s', 'images_client/39/structure/xxx/'); 
+0

謝謝,那真是太棒了! – EricP 2010-01-23 22:06:42

0

嘗試[^/],而不是\S

/images_client\/39\/structure\/([^\/]+)\//