How to remove single quotes from string in php

青灯夜游
Release: 2023-03-16 17:40:01
Original
3399 people have browsed it

Two implementation methods: 1. Use str_replace() to replace all single quotes in the string with empty characters, the syntax is "str_replace("'","", string)"; 2. Use preg_replace() to execute the regular expression "/\'/" to search for all single quotes in the string and replace them with empty characters. The syntax is "preg_replace("/\'/","", characters string)".

How to remove single quotes from string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In php, you can use the str_replace() function Or preg_replace() function to remove single quotes in the string, just replace the single quotes in the string with null characters.

Method 1: Use the str_replace() function to replace single quotes in the string with empty characters

str_replace() function replaces some characters in the string (size-sensitive Write).

str_replace(find,replace,string,count)
Copy after login
ParametersDescription
findRequired . Specifies the value to look for.
replaceRequired. Specifies a value that replaces the value in find.
stringRequired. Specifies the string to be searched for.
count Optional. A variable counting the number of substitutions.

# Just set the first parameter to a single quote "'" and the second parameter to a null character.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "My name is &#39;LiHua&#39;,&#39;20&#39; years old!";
echo "原字符串:".$str."<br>";
$new = str_replace("&#39;","",$str);
echo "去除单引号后:".$new;
?>
Copy after login

How to remove single quotes from string in php

Method 2: Use the preg_replace() function with the regular expression /\'/ to replace the single quotes in the string with Null character

preg_replace function performs a regular expression search and replace.

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
Copy after login

Search for the part matching pattern in subject and replace it with replacement.

Parameter description:

  • $pattern: The pattern to be searched, which can be a string or a string array.

  • $replacement: String or array of strings used for replacement.

  • $subject: The target string or string array to be searched and replaced.

  • $limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).

  • $count: Optional, the number of times the replacement is performed.

Just use the preg_replace() function to execute the regular expression /\'/ to search for all single quotes in the string and replace them with empty Just use the characters

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = "My name is &#39;LiHua&#39;,&#39;20&#39; years old!";
echo "原字符串:".$str."<br>";
$new = preg_replace("/\&#39;/","",$str);
echo "去除单引号后:".$new;
?>
Copy after login

How to remove single quotes from string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove single quotes from string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template