2016-02-08 17 views

回答

4

你也可以試試這個 -

$test = "aaa.bbb.ccc.gif"; 

$temp = explode('.', $test); 

unset($temp[count($temp) - 1]); 

echo implode('.', $temp); 

O/P

aaa.bbb.ccc 

strpos - 查找字符串

一個字符串的第一個出現的位置

您需要使用strrpos

strrpos - 查找字符串的最後一個出現的位置在一個字符串

$test = "aaa.bbb.ccc.gif"; 
$test = substr($test, 0, strrpos($test, ".")); 
echo $test; 

O/P

aaa.bbb.ccc 
9

你可以利用pathinfo()的功能獲取點之前的所有內容

$str = "aaa.bbb.ccc.gif"; 
echo pathinfo($str, PATHINFO_FILENAME); // aaa.bbb.ccc 
+0

'pathinfo'對於文件來說是最好的。 –

+0

@Andrew,有趣的解決方案!) – maximkou

+1

@Sougata真的,我提出這個解決方案的原因是,我看到字符串以.gif結尾,對我來說,每一個字符串接受函數只是一種操縱字符串的方法:D – Andrew

相關問題