方法:1、利用str_replace()函数,语法“str_replace("'",'"',字符串)”;2、利用preg_replace()函数配合正则表达式“/\'/”,语法“preg_replace('/\'/', '"',字符串)”。
本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
php将单引号转成双引号的方法
方法1:利用str_replace()函数
str_replace() 函数替换字符串中的一些字符(区分大小写)。
只需要使用该函数在字符串中查找单引号并将其替换为双引号即可。
注:同种类型引号不能嵌套(单引号内不能包含单引号,双引号内不能包含双引号),可采取"外双内单"或者"外单内双"的格式
示例:
<?php
header('content-type:text/html;charset=utf-8');
$param = "{'id':'12', 'name':'hi'}";
echo "原字符串:".$param."<br>";
$new = str_replace("'",'"',$param);
echo "新字符串:".$new;
?>
方法2:利用preg_replace()函数配合正则表达式来将单引号转为双引号
preg_replace 函数执行一个正则表达式的搜索和替换。
示例:
<?php
header('content-type:text/html;charset=utf-8');
$param = "{'id':'12', 'name':'hi', 'ang':'23'}";
echo "原字符串:".$param."<br>";
$new = preg_replace('/\'/', '"', $param);
echo "新字符串:".$new;
?>