Mac
V2EX  ›  PHP

这两种字符串有什么区别?

  •  
  •   Mac · Jun 30, 2016 · 3219 views
    This topic created in 3783 days ago, the information mentioned may be changed or developed.
    $text='a\tb';

    $text1='a'."\t".'b';

    QRcode::png($text);

    最终输出 TEXT 和 TEXT1 是不一样的
    6 replies  •  2016-06-30 18:47:48 +08:00
    hjxx
    hjxx
    Jun 30, 2016❤️1
    1
    双引号 \t 会被解析成 tab
    单引号 \t 不解析
    Mac
    Mac
    OP
    Jun 30, 2016
    2
    @hjxx 现在有个问题了,我一个很长的字符串,中间有 N 多;号,数量不定,要怎么把他们替换成可以被解析的 tab ?
    daniellu
    daniellu
    Jun 30, 2016❤️1
    3
    正则表达式啊, preg_replace.
    Mac
    Mac
    OP
    Jun 30, 2016
    4
    @daniellu 搞定,原来正则里也要双引号,感谢两位
    raincious
    raincious
    Jun 30, 2016❤️1
    5
    @Mac

    $str = str_replace(';', "\t", $str);

    http://php.net/manual/en/function.str-replace.php

    PHP 里,使用双引号包裹的字符串意味着 **解析器** 会对其进行解析,包括将转义字符转义。

    单引号则表示原字符,其中的内容不会被解析器解析。这就是为什么你那两个字符串“看起来”一样,其实不一样。
    zingl
    zingl
    Jun 30, 2016
    6
    有 php 之前、至少从 shell 开始就是双引号转义单引号不转义了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1476 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 09:52 · PVG 17:52 · LAX 01:52 · JFK 04:52
    ♥ Do have faith in what you're doing.