How does PHP remove single quotes from strings

Use str_replace() to replace all quotes in a string with empty characters, using the syntax “str_replace(“‘”,””, string)”; 2. Use preg_replace() to run the regular expression “/\’/” to search for all single quotes in the string and replace them with empty characters, using the syntax “preg_replace(“/\’/”,””, string)”.

In PHP, you can use the str_replace() function or the preg_replace() function to remove single quotes from a string by simply replacing all single quotes in the string with null characters.

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

The str_replace() function replaces some characters in the string (case sensitive).

<?php
header('content-type:text/html; charset=utf-8');
$STR = "My name is 'LiHua', '20' years old!" ;
Echo "original string: ".$STR."
";
$new = str_replace("'","",$str);
Echo "after removing single quotes: ".$new;
? >

Method 2: Use the preg_replace() function along with the regular expression /\’/ to replace single quotes in a string with nulls

The preg_replace function performs a regular expression search and replace.

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
<!--?</span--> php
header('content-type:text/html; charset=utf-8');
$STR = "My name is 'LiHua', '20' years old!" ;
Echo "original string: ".$STR."
";
$new = preg_replace("/\'/","",$str);
Echo "after removing single quotes: ".$new;
? >

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish