我需要刪除網址的開頭部分:奇怪的行爲
$test = "price/edit.php";
echo ltrim($test,'price/');
顯示dit.php
這裏有一個鍵盤,如果你想搗鼓:https://codepad.remoteinterview.io/DominantCalmingBerlinPrice
任何想法正在發生什麼?當然,我希望它迴應edit.php
。
我需要刪除網址的開頭部分:奇怪的行爲
$test = "price/edit.php";
echo ltrim($test,'price/');
顯示dit.php
這裏有一個鍵盤,如果你想搗鼓:https://codepad.remoteinterview.io/DominantCalmingBerlinPrice
任何想法正在發生什麼?當然,我希望它迴應edit.php
。
ltrim
刪除找到的所有文字,考慮以下因素:
$test = 'price/edit.php';
echo ltrim($test, 'dprice/'); // outputs t.php
對於這種特殊的情況下,你或許應該使用str_replace
。
不幸的是,str_replace不會像我打算的那樣工作....如果我有以下$ test =「test/price/edit.php」,會發生什麼情況。它會從中間去掉價格,我只想把它從字符串中刪除。 – hendr1x
ltrim()
的第二個參數是應該刪除的字符掩碼(字符列表)。 e
是應該刪除的字符,因此它從edit
中刪除。
有,你可以使用很多字符串操作,但是因爲這是一個文件名/文件路徑正確的工具是一個文件系統功能,basename():
echo basename($test);
有關文件路徑支票存入pathinfo()更多信息。
您好我有前一段時間所面臨的同樣的問題,發現這個解決方案中使用它,如果它適合你的需要
<?php
$test = "price/edit.php";
echo ltrim(ltrim($test,'price'),'/');
output
edit.php
,但我必須說,你應該使用basename
所有類型的問題
<?php
$test = "project/price/edit.php";
// echo ltrim(ltrim($test,'price'),'/');// this will give oject/price/edit.phpedit.php
echo basename($test); // and it will generate edit.php
嘗試我的解決方案 – user1234