php ltrim()只删除整个单词

时间:2014-06-06 10:29:11

标签: php trim

所以我有这个字符串'/var/www/html/includes/default_includes.php' 我希望它只是'/var/www/html/'

当使用ltrim时,它会变成'/var/www/htm',我不想要它。 我知道它是因为ltrim删除了单个章程而不是单词,但我怎么才能删除'includes/default_includes.php'

哎呀忘了说字符串也可以是'/home/user1/includes/www/includes/default_includes.php',然后应该变成'/home/user1/includes/www/'

我搜索了很多,但无法找到答案。

1 个答案:

答案 0 :(得分:0)

这在很大程度上取决于你要做的事情。

<?php
$sString = '/var/www/html/includes/default_includes.php';
// I could guess where you are getting this from, but that would be daft

$aParts = explode('/', $sString);

$aParts = array_diff($aParts, array('includes', 'default_includes.php'));

$sString = implode('/', $aParts);

或者,我认为你只是想尝试获取根目录

<?php
$sString = '/var/www/html/includes/default_includes.php';
// I could guess where you are getting this from, but that would be daft

$aParts = explode('/', $sString);

$aParts = array_slice($aParts, 0, -2);

$sString = implode('/', $aParts);