替换方法:1、用preg_replace()函数配合正则表达式“/\"/”,语法“preg_replace('/\"/',"'",字符串)”;2、利用str_replace()函数,语法“str_replace('"',"'",字符串)”。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
php将双引号替换为单引号的方法
方法1:利用preg_replace()函数配合正则表达式来将双引号转为单引号
preg_replace 函数执行一个正则表达式的搜索和替换。
立即学习“PHP免费学习笔记(深入)”;
使用的正则表达式为:
/\"/
示例:
<?php
header('content-type:text/html;charset=utf-8');
$param = '{"id":"12", "name":"hi"}';
echo "原字符串:".$param."<br>";
$new = preg_replace('/\"/', "'", $param);
echo "新字符串:".$new;
?>
方法2:利用str_replace()函数
str_replace() 函数替换字符串中的一些字符。
示例:
<?php
header('content-type:text/html;charset=utf-8');
$param = '{"id":"12", "name":"hi","ang":"23"}';
echo "原字符串:".$param."<br>";
$new = str_replace('"',"'",$param);
echo "新字符串:".$new;
?>
推荐学习:《PHP视频教程》
以上就是php怎么将双引号替换为单引号的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号