First of all, it is always better to check for the unwanted characters and get back to the user, than silently stripping them. Say, a user added a quote to their password, you removed it, and so they won't be able to login at all! So it's better to check and tell the user instead:

if (!ctype_alnum($username)) { 
    $errors[] = "Only letters and numbers allowed in username";
}
...
if ($errors) {
    echo "You've got some errors, please fix them: ". implode("<br>", $errors);
} else {
    // proceed with normal flow

But in this specific case I would advise against replacing any characters. Imagine Shaquille "Shaq" O'Neal is going to register on your site. What's the point in stripping him of all those quotes? Let alone the password, where use of punctuation is strongly encouraged.

After all, those quotes don't do any harm, if you properly handle them.

  • for HTML, simply use htmlspecialchars() with ENT_QUOTES attribute:

      Your username is: <?= htmlspecialchars($username, ENT_QUOTES) ?><br />
    
  • for SQL, use prepared statements to avoid any problems with quotes

But just for sake of literal answer, here is how to remove quotes (or any other characters and even multi-character substrings) in PHP

$yourVariable = "scor\"pi'on";
$substringsToRemove = ['\'', '"'];
$yourVariable = str_replace($substringsToRemove, "", $yourVariable);
Answer from ScoRpion on Stack Overflow
Discussions

php - str_replace double and single quotes - Stack Overflow
Can you show me your input string? And do you have other str_replaces? 2013-08-22T17:41:37.307Z+00:00 ... Problem solved. I was viewing the output in my browser's console, which for some reason was showing the backslash for double quotes, but not singles. More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 23, 2013
php - str_replace for double quotes simply is not working - Stack Overflow
I've used str_replace for many characters and it works perfectly. For the life of me it does not work with double quotes. It simply doesn't do anything: ... This does not remove double quotes. Is there some Apache or PHP setting that stops this from working? More on stackoverflow.com
๐ŸŒ stackoverflow.com
php string replace quotes - Stack Overflow
Hello I am trying to get all single quotes to be double quotes using the php str_replace however it does not seem to be working no matter what I do, suggestions $page = str_replace("/'/", '/"/', $... More on stackoverflow.com
๐ŸŒ stackoverflow.com
str replace - PHP str_replace quotes and double quotes - Stack Overflow
I have a textarea and the user can type in single quotes and double quotes, but before I insert this data in the database, I would like to replace the quotes with \' and double quotes \" I tried to... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 22, 2014
๐ŸŒ
Bytes
bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ php
Replacing double quotes with single quotes - Post.Byes
November 1, 2015 - Example: <? $text = "This is a string with 'single quotes' and " . '"double quotes" in it.'; echo $text."\n"; echo str_replace('"' ,"'",$text)."\n "; echo htmlentities($t ext,ENT_QUOTES) ; ?> Yields: php -q quotetest.php This is a string with 'single quotes' and "double quotes" in it.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.str-replace.php
PHP: str_replace - Manual
Be aware that if you use this for ... gotcha to watch out for: // Remove all double characters $string="1001011010"; $string=str_replace(array("11","00"),"",$string); // Output: "110010" $string="<ht<html>ml> Malicious code ...
Find elsewhere
๐ŸŒ
php.cn
m.php.cn โ€บ 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-PHP Problem-php.cn
October 12, 2020 - $str=" [{\"1\":\"2\"}] "; echo $str; //่พ“ๅ‡บ[{"1":"2"}] $str=str_replace("\"","&#39;",$str); echo $str; //่พ“ๅ‡บ[{&#39;1&#39;:&#39;2&#39;}] ... The above is the detailed content of How to replace double quotes with single quotes in php.
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ string replacing (str_replace) double quotes in text on a wikipedia page.
r/PHPhelp on Reddit: String replacing (str_replace) double quotes in text on a Wikipedia page.
April 20, 2014 -

Hey r/PHPhelp

For one of my class assignments we are coding "parasites" that modify existing code. I have chosen Wikipedia as my source code. I would like to replace every word that is placed in double quotes with a different text (e.g "house of construction" gets replaced with "man of steel"). My current code however is replacing all double quotes in the entire code instead of just those in the text. I hope that makes sense. This is what I have so far (not working as it should):

<?php

$homepage = file_get_contents('wikipedia.html');

$example_tweet[0] = 'I want to make things so beautiful';

$homepage = str_replace('""', $example_tweet[0], $homepage);

echo($homepage);

?>

๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ php โ€บ remove quotes from string in php
Remove Quotes from String in PHP - Java2Blog
October 10, 2023 - Use the str_replace() function to remove quotes from String in PHP. ... We used str_replace() to remove double quotes and single quotes from the String.
Top answer
1 of 3
49

There are many characters that look like quotation marks, most of them are used infrequently. The ones that are used more often are these three:

"   U+0022 QUOTATION MARK
โ€œ   U+201C LEFT DOUBLE QUOTATION MARK
โ€   U+201D RIGHT DOUBLE QUOTATION MARK

Some rarer ones are FULLWIDTH QUOTATION MARK, the DITTO MARK, the DOUBLE PRIME, the DOUBLE PRIME QUOTATION MARK, and so on. The Unicode.org "confusables" tool finds 15 characters similar to ".

Why don't you copy and paste the offending character here so we can identify it? Or you could use the HEX function to get the hexadecimal encoding of the character, that's another way of identifying it.

Update The unicode.org confusables utility seems to be down, but the data is available as a text file. The current list of characters that are "confusable" with double quote are:

1CD3 ;  0027 0027 ; MA  #* ( แณ“ โ†’ '' ) VEDIC SIGN NIHSHVASA โ†’ APOSTROPHE, APOSTROPHE # โ†’โ€ณโ†’โ†’"โ†’
0022 ;  0027 0027 ; MA  #* ( " โ†’ '' ) QUOTATION MARK โ†’ APOSTROPHE, APOSTROPHE   # 
FF02 ;  0027 0027 ; MA  #* ( ๏ผ‚ โ†’ '' ) FULLWIDTH QUOTATION MARK โ†’ APOSTROPHE, APOSTROPHE # โ†’โ€โ†’โ†’"โ†’
201C ;  0027 0027 ; MA  #* ( โ€œ โ†’ '' ) LEFT DOUBLE QUOTATION MARK โ†’ APOSTROPHE, APOSTROPHE   # โ†’"โ†’
201D ;  0027 0027 ; MA  #* ( โ€ โ†’ '' ) RIGHT DOUBLE QUOTATION MARK โ†’ APOSTROPHE, APOSTROPHE  # โ†’"โ†’
201F ;  0027 0027 ; MA  #* ( โ€Ÿ โ†’ '' ) DOUBLE HIGH-REVERSED-9 QUOTATION MARK โ†’ APOSTROPHE, APOSTROPHE    # โ†’โ€œโ†’โ†’"โ†’
2033 ;  0027 0027 ; MA  #* ( โ€ณ โ†’ '' ) DOUBLE PRIME โ†’ APOSTROPHE, APOSTROPHE # โ†’"โ†’
2036 ;  0027 0027 ; MA  #* ( โ€ถ โ†’ '' ) REVERSED DOUBLE PRIME โ†’ APOSTROPHE, APOSTROPHE    # โ†’โ€ตโ€ตโ†’
3003 ;  0027 0027 ; MA  #* ( ใ€ƒ โ†’ '' ) DITTO MARK โ†’ APOSTROPHE, APOSTROPHE   # โ†’โ€ณโ†’โ†’"โ†’
05F4 ;  0027 0027 ; MA  #* ( โ€Žืดโ€Ž โ†’ '' ) HEBREW PUNCTUATION GERSHAYIM โ†’ APOSTROPHE, APOSTROPHE   # โ†’"โ†’
02DD ;  0027 0027 ; MA  #* ( ห โ†’ '' ) DOUBLE ACUTE ACCENT โ†’ APOSTROPHE, APOSTROPHE  # โ†’"โ†’
02BA ;  0027 0027 ; MA  # ( สบ โ†’ '' ) MODIFIER LETTER DOUBLE PRIME โ†’ APOSTROPHE, APOSTROPHE  # โ†’"โ†’
02F6 ;  0027 0027 ; MA  #* ( หถ โ†’ '' ) MODIFIER LETTER MIDDLE DOUBLE ACUTE ACCENT โ†’ APOSTROPHE, APOSTROPHE   # โ†’หโ†’โ†’"โ†’
02EE ;  0027 0027 ; MA  # ( หฎ โ†’ '' ) MODIFIER LETTER DOUBLE APOSTROPHE โ†’ APOSTROPHE, APOSTROPHE # โ†’โ€ณโ†’โ†’"โ†’
05F2 ;  0027 0027 ; MA  # ( โ€Žืฒโ€Ž โ†’ '' ) HEBREW LIGATURE YIDDISH DOUBLE YOD โ†’ APOSTROPHE, APOSTROPHE  # โ†’โ€Žื™ื™โ€Žโ†’
2 of 3
3

I was searching for the Double low quote character but it was not listed in the above answers. Finally found it, so I'm sharing it to save some time:

โ€ž A nice quotation โ€

โ€ž = Double low quote / &bdquo; / &#8222; / &#x201E; / U+201E

โ€ = Right Double Quotation / &rdquo; / &#8221; / &#x201D; / U+201D

๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ php coding โ€บ php coding help
[SOLVED] escaping single quotes, double quotes, and semicolons with STR_REPLACE() - PHP Coding Help - PHP Freaks
April 9, 2007 - i have this error code unexpected T STRING on this line: str_replace(addslashes('onmouseover="showImg('name', 'tabla_novedades2', 's_asc.png', 'img1');"'), addslashes('onmouseover="showImg('name', 'tabla_novedades2', 's_desc.png', 'img1');"'), $td_name); ...