2012-12-06 34 views
1

我的網址是這樣的:如何用php preg_replace更改url的最後一段?

/img/products/loop/blabla/81.jpg 
/img/products/loop/blabla/asasd/811234.jpg 
/img/productsasdasd.jpg 

我想替換這個網址是這樣的:

/img/products/loop/blabla/null.jpg 
/img/products/loop/blabla/asasd/null.jpg 
/img/null.jpg 

如何做呢?

回答

1
$string="/img/products/loop/blabla/81.jpg"; 
    echo preg_replace("/[A-Za-z0-9]+[.]/","null.",$string); 
0

下面是做這件事的一種方式,雖然您可能還需要看看parse_url

<?php 

$urls = array(
    '/img/products/loop/blabla/81.jpg', 
    '/img/products/loop/blabla/asasd/811234.jpg', 
    '/img/productsasdasd.jpg' 
); 

foreach ($urls as $url) { 
    var_dump(preg_replace('#/\w+(\.\w+)$#', '/null$1', $url)); 
}