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

How to replace double quotes with single quotes in php

青灯夜游
青灯夜游Original
2022-04-25 20:13:323085browse

Replacement method: 1. Use the preg_replace() function with the regular expression "/\"/", the syntax "preg_replace('/\"/',"'", string)"; 2. Use str_replace() function, syntax "str_replace('"',"'",string)".

How to replace double quotes with single quotes in php

The operating environment of this tutorial: windows7 system, PHP7. Version 1, DELL G3 computer

php method to replace double quotes with single quotes

Method 1: Use preg_replace() The function works with a regular expression to convert double quotes into single quotes

preg_replace function performs a regular expression search and replace.

The regular expression used is: /\"/

Example:

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

How to replace double quotes with single quotes in php

Method 2: Using str_replace() Function

str_replace() function replaces some characters in a string.

Example:

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

How to replace double quotes with single quotes in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace double quotes with single 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