maget0ron
maget0ron

Reputation: 347

How to remove double quotes from a string

This is my string:

$a='"some text';`

How can I remove the double quote so my output will look like this?

some text`

Upvotes: 34

Views: 137375

Answers (6)

Pri Nce
Pri Nce

Reputation: 711

There have two ways you can remove double quotes also third brackets from an array string.

Sample 1:

$ids = ["1002","1006","1005","1003","1004"];
$string = str_replace(array('[',']'),'',$ids);
$newString = preg_replace('/"/i', '', $string);
return $newString;

Sample 2:

function removeThirdBrackets($string)
 {
    $string = str_replace('[', '', $string);
    $string = str_replace(']', '', $string);
    $string = str_replace('"', '', $string);
    return $string;
 }

Output:

1002,1006,1005,1003,1004

Upvotes: 1

Sharma Vikram
Sharma Vikram

Reputation: 2470

There are different functions are available for replacing characters from string below are some example

$a='"some text';
echo 'String Replace Function<br>';
echo 'O/P : ';
echo $rs =str_replace('"','',$a);
echo '<br>===================<br>';
echo 'Preg Replace Function<br>';
echo 'O/P : ';  
echo preg_replace('/"/','',$a);
echo '<br>===================<br>';
echo 'Left Trim Function<br>';
echo 'O/P : ';  
echo ltrim($a, '"');
echo '<br>===================';

Here is the output.

Output image

Upvotes: 2

Aman Garg
Aman Garg

Reputation: 3290

if string is: $str = '"World"';

ltrim() function will remove only first Double quote.

Output: World"

So instead of using both these function you should use trim(). Example:

$str = '"World"';
echo trim($str, '"');

Output-

World

Upvotes: 23

No Results Found
No Results Found

Reputation: 102735

Probably makes the most sense to use ltrim() since str_replace() will remove all the inner quote characters (depends, maybe that's what you want to happen).

ltrim — Strip whitespace (or other characters) from the beginning of a string

echo ltrim($string, '"');

If you want to remove quotes from both sides, just use regular trim(), the second argument is a string that contains all the characters you want to trim.

Upvotes: 4

Asciiom
Asciiom

Reputation: 9975

Use str_replace

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

Upvotes: 2

John Conde
John Conde

Reputation: 219794

str_replace()

echo str_replace('"', '', $a);

Upvotes: 45

Related Questions