替换方法:1、用preg_replace()函数配合正则表达式“/\"/”,语法“preg_replace('/\"/',"'",字符串)”;2、利用str_replace()函数,语法“str_replace('"',"'",字符串)”。
本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
php将双引号替换为单引号的方法
方法1:利用preg_replace()函数配合正则表达式来将双引号转为单引号
preg_replace 函数执行一个正则表达式的搜索和替换。
使用的正则表达式为:/\"/
示例:
<?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;
?>