我有一个输出路径的功能,这里有一些结果:PHP字符串格式化(SUBSTR)
http://server.com/subdirectory/subdiretory/2021/12/file.txt
http://server.com/subdirectory/subdiretory/something/else/2016/16/file.txt
http://server.com/subdirectory/subdiretory/2001/22/file.txt
C:\totalmess/mess\mess/2012/06/file.txt
我希望削减一切从这些除外文件名和两个父目录,所以上述的那些看起来像:
/2021/12/file.txt
/2016/16/file.txt
/2001/22/file.txt
/20012/06/file.txt
所以基本上我必须从最后找到第三个“/”,并显示它之后的一切。
我不知道PHP不太好,但我想这是很容易实现与SUBSTR(),stripos函数()和strlen的(),所以:
$string ="http://server.com/subdirectory/subdiretory/2001/22/file.txt"
$end = strlen($string);
$slash = // How to get the right slash using stripos()?
$output = substr($string, $slash, $end);
echo $output;
这是正道这样做,或者有另一个内置函数可以搜索字符串中的第n个符号?
+1殴打我对我的答案:) – Cfreak
你,当然需要,如果你这样做是为了得到一个字符串返回重新破灭。 (另外,你不需要传递'3'长度的参数; array_slice默认会包含到数组的最后。) –
@John,感谢关于不必要的参数的注释,我实际上已经捕获了'implode '在你的笔记之前;) – Shad