Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove quotes from a string?

Tags:

php

quotes

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

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

like image 350
James Avatar asked Nov 19 '10 18:11

James


People also ask

How do you remove quotes from a string in Python?

To erase Quotes (“”) from a Python string, simply use the replace() command or you can eliminate it if the quotes seem at string ends.

How do you remove quotes from a string in react?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') .

How do you remove a quote from a variable?

A single line sed command can remove quotes from start and end of the string. The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.


2 Answers

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

like image 154
J V Avatar answered Oct 07 '22 03:10

J V


You can do the same in one line:

str_replace(['"',"'"], "", $text) 
like image 41
Julio Popócatl Avatar answered Oct 07 '22 02:10

Julio Popócatl