Maureen Moore
Maureen Moore

Reputation: 1127

Remove quotation marks from variable

I'm trying to trim away the quotation marks from a PHP variable in CakePHP. I've tried trim(), substr(), ereg_replace(), and str_replace(), but nothing affects the quotation marks. When I use substr like this:

substr($comma_separated, 1, -1);

it removes the first and the last letter, but not the quotation marks. The string is

$comma_separated = "[email protected]','[email protected]"

and this is an invalid email address for CakeEmail. I've also tried

$comma_separated = ereg_replace('"', "", $comma_separated);

and

$comma_separated = str_replace('"', '', $comma_separated);

I wasn't specific enough. It's the double quotation marks that I'm trying to remove. Not the single ones.

I've tried all this and they look like the normal quotation marks $comma_separated = ereg_replace('"', "", $comma_separated); //no change in output $comma_separated = str_replace('"', '', $comma_separated); //no change in output $comma_separated = substr($comma_separated,1,-1); //outputs the last and first letter removed $comma_separated = trim($comma_separated,'"'); //no change in output

Upvotes: 0

Views: 2142

Answers (3)

Gar
Gar

Reputation: 862

try this please :

$comma_separated = str_replace('\'', '', $comma_separated);

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227190

Your str_replace isn't working because you are searching for the wrong quotation marks. Your string has single quotes, so why are you searching for double quotes?

$comma_separated = str_replace("'", '', $comma_separated);

You can use this to remove all the quotes from the string.

Upvotes: 2

Ry-
Ry-

Reputation: 224859

You have one string separated by ','. A strange separator, but you can still explode on that:

$addresses = explode("','", $comma_separated);

Upvotes: 2

Related Questions