我只想刪除第三個斜槓前的所有字符。刪除第三個斜槓前的字符
例子:
C:/wamp/www/project/modules/File
becomes:
project/modules/File
我會感激的幫助。謝謝。
我只想刪除第三個斜槓前的所有字符。刪除第三個斜槓前的字符
例子:
C:/wamp/www/project/modules/File
becomes:
project/modules/File
我會感激的幫助。謝謝。
一個優雅的解決方案我想到的是:
$text = "C:/wamp/www/project/modules/File";
$arr = explode("/", $text);
//shifting array three times, increase if necessary
array_shift($arr);array_shift($arr);array_shift($arr);
//implode and return
return implode("/", $arr);
簡單而簡單。
<?
$str = 'C:/wamp/www/project/modules/File';
if (preg_match('%.*?/.*?/.*?/(.*)%', $str, $regs)) {
$str = $regs[1];
}
echo $str;
?>
Result: project/modules/File
嘗試下面的代碼,我希望這會幫助你。
$str = "C:/wamp/www/project/modules/File";
$positions = mb_stripos_all($str,"/");
echo substr($str,$positions[2]+1);
function mb_stripos_all($haystack, $needle) {
$s = 0;
$i = 0;
while(is_integer($i)) {
$i = strpos($haystack, $needle, $s);
if(is_integer($i)) {
$aStrPos[] = $i;
$s = $i + strlen($needle);
}
}
if(isset($aStrPos)) {
return $aStrPos;
} else {
return false;
}
}
$text="C:/wamp/www/project/modules/File";
$pieces = explode("/", $text);
$i=0;
foreach ($pieces as $valor) {
$i++;
if($i>3)
echo $valor.'/';
}
回報
project/modules/File/
$text="C:/wamp/www/project/modules/File";
$pieces = explode("/", $text);
$len=sizeof($pieces);
for($i=3;$i<$len; $i++)
{
$path.=$pieces[$i]."/";
}
$path1 = substr($path, 0, -1);
echo $path1;
I think this will print how long contents after file..
try it...
我們不在乎你想要什麼。告訴我們你已經嘗試了什麼,然後制定一個我們可以回答的問題。 –
對於所有這樣的字符串,C:/ wamp/www /是否保持不變? –
Sverri M. Olsen,你可以通過閱讀我的文章知道我在問什麼。這是你獲得額外代表的方式嗎? – kimbarcelona