gpt4 book ai didi

php - str_replace 双引号和单引号

转载 作者:行者123 更新时间:2023-12-02 21:52:07 27 4
gpt4 key购买 nike

我有一个简单的 PHP 脚本,应该用反斜杠转义单引号和双引号。这是我的代码:

$output = str_replace('"','\"',$input);
$output = str_replace("'","\'",$output);
return $output;

问题是,它只转义双引号,所以像

这样的字符串
"This" is a 'string'

解析为

\"This\" is a 'string'

不是

\"This\" is a \'string\'

如果我将第二行更改为

$output = str_replace("'","asdf",$output);

我明白了

\"This\" is a asdfstringasdf

知道出了什么问题吗?

谢谢

最佳答案

我的代码没有问题,我的测试如下:

<?php

$input = '"This" is a '."'".'String'."'";
echo $input.'<br />';
//Echos "This" is a 'String'

$output = str_replace('"','\"',$input);
$output = str_replace("'","\'",$output);
echo $output;
//Echos \"This\" is a \'String\'

已编辑

现在无关紧要,OP弄清楚了:D

关于php - str_replace 双引号和单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18387016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com