如何用单引号替换双引号

问题描述 投票:30回答:8

如何使用PHP替换""(我认为它被称为双引号)与''(我认为它称为单引号)?

php regex quotes
8个回答
82
投票
str_replace('"', "'", $text);

或重新分配

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

6
投票

使用

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

4
投票

试试preg_replace,

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

3
投票

你可以使用str_replace,尝试使用http://php.net/manual/en/function.str-replace.php它包含很多php文档。

<?php

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

3
投票

试试strtr,

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

2
投票

对于PHP 5.3.7

$str = str_replace('&quot;','&#39;',$str);

要么

$str = str_replace('&quot;',"'",$str);

对于PHP 5.2

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

0
投票

我喜欢使用中间变量:

$OutText = str_replace('"',"'",$InText);

此外,您应该有一个Test.php文件,您可以尝试填写:

$QText = 'I "am" quoted';
echo "<P>QText is: $QText";
$UnQText = str_replace ('"', '', $QText);
echo "<P>Unquoted is: $UnQText";


0
投票

试试这个

//single qoutes
$content = str_replace("\'", "'", $content); 

//double qoutes
$content = str_replace('\"', '"', $content); 
© www.soinside.com 2019 - 2024. All rights reserved.