65
$string = "my text has \"double quotes\" and 'single quotes'";

How to remove all types of quotes (different languages) from $string?

Dennis
  • 7,907
  • 11
  • 65
  • 115
James
  • 42,081
  • 53
  • 136
  • 161

2 Answers2

137
str_replace('"', "", $string);
str_replace("'", "", $string);

I assume you mean quotation marks?

Otherwise, go for some regex, this will work for html quotes for example:

preg_replace("/<!--.*?-->/", "", $string);

C-style quotes:

preg_replace("/\/\/.*?\n/", "\n", $string);

CSS-style quotes:

preg_replace("/\/*.*?\*\//", "", $string);

bash-style quotes:

preg-replace("/#.*?\n/", "\n", $string);

Etc etc...

J V
  • 11,402
  • 10
  • 52
  • 72
  • 9
    It is impossible to parse HTML with regular expressions properly. Never try to do that. – jwueller Nov 19 '10 at 18:47
  • 5
    I agree, but it's also impossible to parse **every** programming language properly... – J V Nov 19 '10 at 18:48
  • 22
    str_replace accepts arrays as well, so str_replace(array('"', "'"), '', $string) will also work. Btw, are you saying that HTML is a programming language? ;-) – GolezTrol Nov 19 '10 at 18:54
  • No of course not but it's still impossible to handle comments from every programming language ;) – J V Nov 19 '10 at 19:01
  • 1
    All your regular expressions are broken. You did not specify delimiters and escaping has been omitted as well. – jwueller Nov 19 '10 at 19:08
  • These were meant as examples; Yes, preg functions require delimiters but they are not part of the regular expression, and the delimiters cause incorrect reading of the forward slashes. – J V Nov 19 '10 at 19:12
  • @J V: They are not part of the regular expressions, but they are part of your examples. And your examples do not work without delimiters. @Gumbo: Thats right. – jwueller Nov 19 '10 at 19:20
  • Parsing HTML can give cancer. see http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 for more details – Reign.85 Jun 20 '14 at 15:32
9

You can do the same in one line:

str_replace(['"',"'"], "", $text)
Julio Popócatl
  • 712
  • 8
  • 16