2013-01-03 61 views
1
標題

請,任何人都可以請告訴我如何替換文件標題images.I'm試圖找出如何更換圖像如何更換爲圖像

$rem = array('-','_','%','+','img','photo','photos','picture','pictures','image','images','jpg','jpeg','png','bmp','copy'); 
$title = str_replace($rem,' ',$title); 
$title = preg_replace("@\\b[a-z0-9]\\b [email protected]", "", $title); 

結果

的標題部分
2015_ferrari_f430_18_1600.jpg -- > 2015-ferrari-f430-18-1600.jpg 
Wimmer-RS-Ferrari-F430-Scuderia-1.jpg --> wimmer-rs-ferrari-f430-scuderia.jpg 
3-Ferrari-F430-Stradale-Spy_1035.jpg -- > ferrari-f430-stradale-spy-1035.jpg 
Auto-Veloce-Ferrari-F430-SVR-3.jpg --> auto-veloce-ferrari-f430-svr.jpg 
Auto-Veloce-Ferrari-F430-SVR-8-1024x692.jpg --> auto-veloce-ferrari-f430-svr-1024x692.jpg 

how to can be it.. 
2015-ferrari-f430.jpg 
wimmer-rs-ferrari-f430-scuderia.jpg 
ferrari-f430-stradale-spy.jpg 
auto-veloce-ferrari-f430-svr.jpg 
auto-veloce-ferrari-f430-svr.jpg 
+0

我真的不明白的標準去除。你想擺脫文件名的最後部分,沒有擴展名,有破折號,數字或x? –

回答

1

改變你的正則表達式模式。

$title = preg_replace("/[_\-\d]+.jpg$/", ".jpg", $title); 

但是,這不會對您的所有字符串工作,因爲你沒有更換的標準模式(例如,它不會爲2015-ferrari-f430_18_1600.jpgAuto-Veloce-Ferrari-F430-SVR-8-1024x692.jpg工作)。

考慮你想要一個破折號(包括1024x768x)後更換任何最後的數字你也可以試試這個:

$title = preg_replace("/(?:-|_)[_x\-\d]+.jpg$/", ".jpg", $title); 
+0

我以前試過 $ title = preg_replace(「[_ \ - \ d] +。jpg $」,「.jpg」,$ title);或 $ title = preg_replace(「(?: - | _)[_ x \ - \ d] +。jpg $」,「.jpg」,$ title); 但我得到的消息 警告:preg_replace函數()[function.preg替換]:未知的修飾詞 '[' 在d:\數據\ autoblogcar \可溼性粉劑內容\主題\項目\ INC \上線replaceimage.php 398 –

+0

@aguswibowo你現在可以檢查。更新了答案。 –

+0

我試過後結果跟 相同 $ title = str_replace($ rem,'',$ title); $ title = preg_replace(「@ \\ b [a-z0-9] \\ b?@i」,「」,$ title); 2015_ferrari_f430_07_1600.jpg - > 2015 - 法拉利f430-07-1600.jpg 2015_ferrari_f430_18_1600.jpg - > 2015 - 法拉利f430-18-1600.jpg FERRARI-F430-法拉利27721006-1024 -587.jpg - > ferrari-f430-ferrari-27721006-1024-587.jpg –