Home >Backend Development >PHP Problem >How to convert single quotes to double quotes in php

How to convert single quotes to double quotes in php

青灯夜游
青灯夜游Original
2022-04-22 15:52:223237browse

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)".

How to convert single quotes to double quotes in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$param = "{&#39;id&#39;:&#39;12&#39;, &#39;name&#39;:&#39;hi&#39;}";
echo "原字符串:".$param."<br>";
$new = str_replace("&#39;",&#39;"&#39;,$param);
echo "新字符串:".$new;
?>

How to convert single quotes to double quotes in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$param = "{&#39;id&#39;:&#39;12&#39;, &#39;name&#39;:&#39;hi&#39;, &#39;ang&#39;:&#39;23&#39;}";
echo "原字符串:".$param."<br>";
$new = preg_replace(&#39;/\&#39;/&#39;, &#39;"&#39;, $param);
echo "新字符串:".$new;
?>

How to convert single quotes to double quotes in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn