How do I replace double quotes with single quotes

91,630

Solution 1

str_replace('"', "'", $text);

or Re-assign it

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

Solution 2

Use

$str = str_replace('"','\'',$str)

Solution 3

Try with preg_replace,

<?php
$string="hello \" sdfsd \" dgf";
echo $string,"\n";
echo preg_replace("/\"/","'",$string);
?>

Solution 4

You can use str_replace, try to use http://php.net/manual/en/function.str-replace.php it contains allot of php documentation.

<?php

echo str_replace("\"","'","\"\"\"\"\" hello world\n");
?>

Solution 5

Try with strtr,

<?php
$string="hello \" sdfsd dgf";
echo $string;
$string = strtr($string, "\"", "'");
echo $string;
?>
Share:
91,630
streetparade
Author by

streetparade

Updated on April 12, 2020

Comments

  • streetparade
    streetparade about 4 years

    How can I replace "" (I think it's called double quotes) with '' (I think its called single quotes) using PHP?

  • SSH This
    SSH This over 11 years
    Yes this is right, but just a newb comment from me. This won't actually change the value of $text by itself, you'll need to set the whole thing equal to $text like this: $text = str_replace('"',"'",$text); Just had to mention this, because I just made this mistake
  • Kellen Stuart
    Kellen Stuart over 7 years
    @SSHThis I did that one time. Took me an hour to figure out what went wrong. Def a newb mistake!