开发者

Trim only the first and last occurrence of a character in a string (PHP)

This is something I could hack together, but I wondered if anybody had a clean solution to my problem. Something that I throw together wont necessarily be very concise or speedy!

I have a string like this ///hello/world///. I need to strip only the first and last slash, none of the others, so that I get a string like this //hello/world//.

PHP's trim isn't quite right right: performing trim($string, '/') will return hello/world.

One thing to note is that the string won't necessarily have any slashes at the beginning or end. Here are a few examples of wh开发者_开发技巧at I would like to happen to different strings:

///hello/world/// > //hello/world//
/hello/world/// > hello/world//
hello/world/ > hello/world

Thanks in advance for any help!


First thing on my mind:

if ($string[0] == '/') $string = substr($string,1);
if ($string[strlen($string)-1] == '/') $string = substr($string,0,strlen($string)-1);


This function acts as the official trim, except that it only trims once.

function trim_once($text, $c) {
    $c = preg_quote($c);
    return preg_replace("#^([$c])?(.*?)([$c])?$#", '$2', $text);
}
php > echo trim_once("||1|2|3|*", "*|");
|1|2|3|
php > echo trim_once("//|1|2|3/", "/");
/|1|2|3


This one is by far the simplest. It matches on ^/ (start slash) and /$ (end slash) and if either are found it is replaced with an empty string. This will work with any character; just replace / in the following regular expression with the character of your choice. Note for the delimiter character I used # instead of / to make it easier to read. This will remove any single first or last / from a string:

$trimmed = preg_replace('#(^/|/$)#', '', $string);

Results:

///hello/world/// > //hello/world//
/hello/world/// > hello/world//
hello/world/ > hello/world


I think this is what you you are looking for:

preg_replace('/\/(\/*[^\/]*?\/*)\//', '\1', $text);


A different regex, using backreferences:

preg_replace('/^(\/?)(.*)\1$/','\2',$text);

This has the advantage that, should you want to use characters other than /, you could do so more legibly. It also forces the / character to begin and end the string, and allows / to appear within the string. Finally, it only removes the character from the beginning if there is a character at the end as well, and vice versa.


Yet another implementation:

function otrim($str, $charlist)
{
 return preg_replace(sprintf('~^%s|%s$~', preg_quote($charlist, '~')), '', $str);
}


It has been more than 6 years old ago, but I'm giving may answer anyway:

function trimOnce($value)
{   
    $offset = 0;
    $length = null;
    if(mb_substr($value,0,1) === '/') {
        $offset = 1;
    }
    if(mb_substr($value,-1) === '/') {
       $length = -1;
    }
    return mb_substr($value,$offset,$length);
}


I wrote this code for trim once characters

function trimOnce( $string, $characters ) {
    if ( ! is_string( $string ) || ! is_string( $characters ) )
        return $string;

    $stringLength     = mb_strlen( $string );
    $charactersLength = mb_strlen( $characters );

    if ( $charactersLength > $stringLength )
        return $string;

    if ( mb_substr( $string, 0, $charactersLength ) == $characters ) {
        $string       = mb_substr( $string, $charactersLength, $stringLength );
        $stringLength = mb_strlen( $string );
    }

    if ( mb_substr( $string, - 1 * $charactersLength ) == $characters )
        $string = mb_substr( $string, 0, $stringLength - $charactersLength );

    return $string;
}

echo trimOnce( trimOnce( '<div><div>test</div></div>', '<div>' ), '</div>' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜