【问题标题】:why my str_replace cannot use in \ symbol in php [duplicate]为什么我的str_replace不能在php中使用\符号[重复]
【发布时间】:2021-07-28 11:47:47
【问题描述】:
$originalMessage = "hello i want to \ this pc";
$replaceMessage = str_replace('\', 'check', $originalMessage );

系统显示如下错误:

解析错误:语法错误,意外 '" ,"' (T_CONSTANT_ENCAPSED_STRING),应为 ')'

【问题讨论】:

    标签: php str-replace


    【解决方案1】:
    str_replace('\', 'check', $originalMessage );
    

    '\' 部分 escapes 结束 ',因此 PHP 仍在等待字符串关闭。

    在一个不错的 IDE 中可以清楚地看到这一点:


    建议转义 \ 以防止它转义 '

    str_replace('\\', 'check', $originalMessage );
    

    你可以测试on this online demo

    【讨论】:

    • 但是当我使用这个 str_replace('\\', 'check', $originalMessage );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2021-11-29
    相关资源
    最近更新 更多