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

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

5
  • 17
    $string has no quotes in it you do realize this?
    – Chris
    Commented Nov 19, 2010 at 18:48
  • what about quotes represented as htmlentities or urlencoded?
    – bcosca
    Commented Nov 19, 2010 at 18:50
  • 2
    Are you talking about natural-language quotes (for example, „) or about programming-language quotes (such as /*...*/)?
    – thejh
    Commented Nov 19, 2010 at 18:57
  • @Chris - does now ! :)
    – Dennis
    Commented Apr 17, 2015 at 13:53
  • If you're just removing them from the start and end, then trim($str, "\"\'")
    – frumbert
    Commented Apr 13, 2020 at 9:38

2 Answers 2

143
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...

8
  • 9
    It is impossible to parse HTML with regular expressions properly. Never try to do that.
    – jwueller
    Commented Nov 19, 2010 at 18:47
  • 5
    I agree, but it's also impossible to parse every programming language properly...
    – J V
    Commented Nov 19, 2010 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
    Commented Nov 19, 2010 at 18:54
  • No of course not but it's still impossible to handle comments from every programming language ;)
    – J V
    Commented Nov 19, 2010 at 19:01
  • 1
    All your regular expressions are broken. You did not specify delimiters and escaping has been omitted as well.
    – jwueller
    Commented Nov 19, 2010 at 19:08
14

You can do the same in one line:

str_replace(['"',"'"], "", $text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.