Home >Backend Development >PHP Problem >How to convert single quotes to double quotes in php
Method: 1. Use the str_replace() function, syntax "str_replace("'",'"', string)"; 2. Use the preg_replace() function with the regular expression "/\'/" , syntax "preg_replace('/\'/', '"', string)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will be single quotes Method of converting to double quotes
Method 1: Use the str_replace() function
str_replace() function to replace some characters in the string ( case sensitive).
Just use this function to find single quotes in a string and replace them with double quotes.
Note: The same type of quotation marks cannot be nested (single quotation marks cannot contain single quotation marks, and double quotation marks cannot contain double quotation marks). You can use "outer double and inner single" or "outer single and inner double" "Format
Example:
<?php header('content-type:text/html;charset=utf-8'); $param = "{'id':'12', 'name':'hi'}"; echo "原字符串:".$param."<br>"; $new = str_replace("'",'"',$param); echo "新字符串:".$new; ?>
Method 2: Use the preg_replace() function with regular expressions to convert single quotes The preg_replace function performs a regular expression search and replace for double quotes
.
Example:
<?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; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert single quotes to double quotes in php. For more information, please follow other related articles on the PHP Chinese website!