34

This is my string:

$a='"some text';`

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

some text`
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
maget0ron
  • 347
  • 1
  • 3
  • 7

6 Answers6

44

str_replace()

echo str_replace('"', '', $a);
John Conde
  • 217,595
  • 99
  • 455
  • 496
22

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
Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Aman Garg
  • 3,122
  • 4
  • 24
  • 32
  • 2
    This is the correct answer. Everyone who suggested str_replace -- that will replace *all* the double quotes. trim($str, '"') will remove double quotes from the beginning and end of the string. – Phl3tch Sep 11 '20 at 14:45
4

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.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
2

Use str_replace

$a = str_replace('"', '', $a);
Asciiom
  • 9,867
  • 7
  • 38
  • 57
2

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

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
  • Please show the result as code instead of a screenshot, see why on [meta](https://meta.stackoverflow.com/a/285557/2257664). – A.L Aug 17 '22 at 10:55
1

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
Pri Nce
  • 576
  • 6
  • 18
  • After getting that output (those numbers separated by comma) `1002,1006,1005,1003,1004`, How do you insert them into an array? – Pathros Sep 21 '22 at 21:37