dongxian4531
2015-05-25 15:28
浏览 27
已采纳

在php中意外输出ltrim

Can anybody explain this unusual output of ltrim

var_dump(ltrim('/btcapi/participation/set-user-event-participation','/btcapi'));

rticipation/set-user-event-participation //output

While expected output has

/participation/set-user-event-participation

图片转代码服务由CSDN问答提供 功能建议

任何人都可以解释ltrim的这种不寻常的输出

  var_dump(  ltrim('/ btcapi / participation / set-user-event-participation','/ btcapi')); 
 
rticipation / set-user-event-participation // output 
   \  n 
 

预期输出 <前> / participation / set-user-event-participation

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douren0558 2015-05-25 15:37
    已采纳

    Use str_replace if you are sure this is the only one occurence in your string.

    $str = '/btcapi/participation/set-user-event-participation';
    echo str_replace('/btcapi', $str); // returns: '/participation/set-user-event-participation'
    

    Or regex if you need replace/remove just the first at the beginning of string.

    $str = '/btcapi/participation/set-user-event-participation';
    preg_replace ('~^/btcapi~', '', $str);
    
    点赞 评论
  • duandian8251 2015-05-25 15:30

    The trim characters are read as individuals, not as a String.

    It just replaces the second / for example because it is a part of the characters.

    Just use str_replace or a custom loop.

    点赞 评论
  • duanrong0738 2015-05-25 15:32

    RTM: http://php.net/ltrim

    the second argument is a character MASK, e.g. characters you want to strip. CHARACTERS, not STRING.

    php > $foo = 'abc123';
    php > echo ltrim($foo, 'abpq');
    c123
    php > echo ltrim($foo, 'a1');
    bc123
      ^---not stripped, because 'bc' are not in the mask.
    php >
    

    PHP will search strip all characters from the left of the string, based on the characters in the mask, until it encounters a character NOT in the mask.

    点赞 评论

相关推荐 更多相似问题