nilai
V2EX  ›  PHP

一个关于 PHP trim 函数的问题

  •  
  •   nilai · Jul 17, 2020 · 4192 views
    This topic created in 2304 days ago, the information mentioned may be changed or developed.
    代码非常简单:
    <?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/xap"
    问题:为什么会把字符串结尾的 i 字符给吞了,




    环境 php 7.3 php5.6 均测试如此
    19 replies  •  2020-07-20 09:38:09 +08:00
    geeglo
    geeglo
    Jul 17, 2020
    1
    trim 的常见误区,的第二参数不会作为整体去移除。而是每个字符单独左右移除
    nilai
    nilai
    OP
    Jul 17, 2020
    2
    @geeglo 若要整体移除呢? 用什么函数代替?
    zachlhb
    zachlhb
    Jul 17, 2020 via Android
    3
    @nilai rtrim
    geeglo
    geeglo
    Jul 17, 2020
    4
    没有,自己写。
    weirdo
    weirdo
    Jul 17, 2020❤️1
    5
    @nilai str_replace preg_replace
    $url = str_replace('.git', '', $url)
    U7Q5tLAex2FI0o0g
    U7Q5tLAex2FI0o0g
    Jul 17, 2020
    6
    手册有说明:
    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
    rming
    Jul 17, 2020
    7
    $url = 'http://10.100.11.5/xapi.git';
    $tmp = preg_replace("/\.git$/", "", $url);
    rming
    rming
    Jul 17, 2020
    8
    preg_replace("/^start|end$/", "", "start content end")
    nilai
    nilai
    OP
    Jul 17, 2020
    9
    @weirdo str_replace 不适合, str_replace 会替换字符串中的所有, trim 功能中只去头尾
    nilai
    nilai
    OP
    Jul 17, 2020
    10
    @rming preg_replace 能实现。
    nilai
    nilai
    OP
    Jul 17, 2020
    11
    @rming 简单写了个函数 这样可好?
    function strim($string,$removestring){
    if (!is_string($string) || !is_string($removestring)){
    return $string;
    }
    $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string);
    return $result;
    }
    nilai
    nilai
    OP
    Jul 17, 2020
    12
    改成这样可能会更好点。
    function strim($string,$removestring=''){
    if (!is_string($string)){
    return $string;
    }
    if (!$removestring){
    return trim($string);
    }
    $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string);
    return $result;
    }
    KasuganoSoras
    KasuganoSoras
    Jul 17, 2020
    13
    @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
    airdge
    Jul 17, 2020❤️2
    14
    这不是很正常
    trim 会循环去除 charlist 里面的字符,直到完全移除
    igithttp://10.100.11.5/xapiggtigigit , git 这个应该也只会输出 http://10.100.11.5/xap
    yc8332
    yc8332
    Jul 17, 2020
    15
    这个是去除字符,不是字符串。。。你要去除字符串要用替换
    WeKeey
    WeKeey
    Jul 17, 2020
    16
    substr pathinfo
    jiehuangwei
    jiehuangwei
    Jul 17, 2020
    17
    有意思,明显没细看函数吧 猜猜这个输出 啥 trim('abc', 'bad')
    sevenzhou1218
    sevenzhou1218
    Jul 17, 2020
    18
    没仔细看手册,看了就不会问这个问题了
    cbasil
    cbasil
    Jul 20, 2020
    19
    去除字符串用 str_replace 或者 preg_replace 啊,trim 一般用来去掉字符串二端的空格
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1727 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 12:28 · PVG 20:28 · LAX 04:28 · JFK 07:28
    ♥ Do have faith in what you're doing.