一个关于 PHP trim 函数的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nilai
V2EX    PHP

一个关于 PHP trim 函数的问题

  •  
  •   nilai 2020-07-17 10:55:41 +08:00 3361 次点击
    这是一个创建于 1965 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码非常简单:
    <?php
    $url='http://10.100.11.5/xapi.git';
    $tmp=trim($url,'.git');
    var_dump($tmp);
    $url='http://10.100.11.5/xapi';
    $tmp=trim($url,'git');
    var_dump($tmp);


    执行后的结果:
    root@debian10:/var/www/html# php 7.php
    string(22) "http://10.100.11.5/xap"
    string(22) "http://10.100.11.5/ap"
    问题:为什么会把字符串结尾的 i 字符给吞了,




    环境 php 7.3 php5.6 均测试如此
    19 条回复    2020-07-20 09:38:09 +08:00
    geeglo
        1
    geeglo  
       2020-07-17 10:59:03 +08:00
    trim 的常见误区,的第二参数不会作为整体去移除。而是每个字符单独左右移除
    nilai
        2
    nilai  
    OP
       2020-07-17 11:04:41 +08:00
    @geeglo 若要整体移除呢? 用什么函数代替?
    zachlhb
        3
    zachlhb  
       2020-07-17 11:05:59 +08:00 via Android
    @nilai rtrim
    geeglo
        4
    geeglo  
       2020-07-17 11:06:13 +08:00
    没有,自己写。
    weirdo
        5
    weirdo  
       2020-07-17 11:06:56 +08:00   1
    @nilai str_replace preg_replace
    $url = str_replace('.git', '', $url)
    U7Q5tLAex2FI0o0g
        6
    U7Q5tLAex2FI0o0g  
       2020-07-17 11:14:31 +08:00
    手册有说明:
    https://www.php.net/manual/en/function.trim.php#refsect1-function.trim-notes

    Note: Possible gotcha: removing middle characters
    Because trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b') seemingly does not.
    rming
        7
    rming  
       2020-07-17 11:21:59 +08:00
    $url = 'http://10.100.11.5/xapi.git';
    $tmp = preg_replace("/\.git$/", "", $url);
    rming
        8
    rming  
       2020-07-17 11:23:37 +08:00
    preg_replace("/^start|end$/", "", "start content end")
    nilai
        9
    nilai  
    OP
       2020-07-17 11:29:37 +08:00
    @weirdo str_replace 不适合, str_replace 会替换字符串中的所有, trim 功能中只去头尾
    nilai
        10
    nilai  
    OP
       2020-07-17 11:30:20 +08:00
    @rming preg_replace 能实现。
    nilai
        11
    nilai  
    OP
       2020-07-17 11:34:38 +08:00
    @rming 简单写了个函数 这样可好?
    function strim($string,$removestring){
    if (!is_string($string) || !is_string($removestring)){
    return $string;
    }
    $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string);
    return $result;
    }
    nilai
        12
    nilai  
    OP
       2020-07-17 11:46:50 +08:00
    改成这样可能会更好点。
    function strim($string,$removestring=''){
    if (!is_string($string)){
    return $string;
    }
    if (!$removestring){
    return trim($string);
    }
    $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string);
    return $result;
    }
    KasuganoSoras
        13
    KasuganoSoras  
       2020-07-17 11:53:56 +08:00
    @nilai #9 不要忘了 str_replace 有 count 参数
    $url = str_replace(".git", "", $url, 1);
    这样就只会替换一次了,另外如果你只是想单纯去掉结尾 .git 的话
    $url = substr($url, -4, 4) == ".git" ? substr($url, 0, strlen($url) - 4) : $url;
    完事了
    airdge
        14
    airdge  
       2020-07-17 11:54:48 +08:00   2
    这不是很正常
    trim 会循环去除 charlist 里面的字符,直到完全移除
    igithttp://10.100.11.5/xapiggtigigit , git 这个应该也只会输出 http://10.100.11.5/xap
    yc8332
        15
    yc8332  
       2020-07-17 11:58:59 +08:00
    这个是去除字符,不是字符串。。。你要去除字符串要用替换
    WeKeey
        16
    WeKeey  
       2020-07-17 12:13:02 +08:00
    substr pathinfo
    jiehuangwei
        17
    jiehuangwei  
       2020-07-17 13:07:26 +08:00
    有意思,明显没细看函数吧 猜猜这个输出 啥 trim('abc', 'bad')
    sevenzhou1218
        18
    sevenzhou1218  
       2020-07-17 13:29:40 +08:00
    没仔细看手册,看了就不会问这个问题了
    cbasil
        19
    cbasil  
       2020-07-20 09:38:09 +08:00
    去除字符串用 str_replace 或者 preg_replace 啊,trim 一般用来去掉字符串二端的空格
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5544 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 38ms UTC 03:19 PVG 11:19 LAX 19:19 JFK 22:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86