php php字符串替换引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10200127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:37:45  来源:igfitidea点击:

php string replace quotes

phpstr-replace

提问by kqlambert

Hello I am trying to get all single quotes to be double quotes using the php str_replace however it does not seem to be working no matter what I do, suggestions

您好,我正在尝试使用 php str_replace 将所有单引号变为双引号,但是无论我做什么,它似乎都不起作用,建议

$page = str_replace("/'/", '/"/', $page);

回答by BluesRockAddict

Update: I'd agree with others that the following is an easier-to-read alternative for most folks:

更新:我同意其他人的看法,以下是大多数人更易于阅读的替代方案:

$page = str_replace("'", '"', $page);

My original answer:

我的原答案:

$page = str_replace(chr(39), chr(34), $page);

回答by alex

You don't need to escape the quote character (in fact it is \, not /, unless you were confused with the standard regex delimiters) if the string isn't delimited with the same character.

如果字符串没有用相同的字符分隔,则不需要对引号字符进行转义(实际上它是\,不是/,除非您与标准正则表达式分隔符混淆)。

$page = str_replace("'", '"', $page);

回答by kqlambert

This should work:

这应该有效:

str_replace("'",'"',$text);

回答by Patrick Lorio

$page = str_replace("'", "\"", $page);

回答by Sampo Sarrala - codidact.org

I think you should do replacements with preg_replace();

我认为你应该用preg_replace();做替换

$str = "'Here 'it' goes'";
echo preg_replace("/'/", '"', $str);

回答by Robert Wilson

This works. You actually don't need any escaping character.

这有效。你实际上不需要任何转义字符。

$page = str_replace("'", '"', $page);

回答by 0b10011

You only need the start and end /for preg_...()(and other regex) functions. For basic functions such as str_replace, simply use the characters:

你只需要在开始和结束/preg_...()(和其他正则表达式)功能。对于诸如 之类的基本功能str_replace,只需使用字符:

str_replace("'", '"', $text);