-1
<?php
$str='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace=''';
$string= str_replace($search,$replace,$str);
echo $string;
?>

当我回显 $string 结果没有从 " 转换为 ' 时,如何解决它

4

4 回答 4

3

在您的原始代码中,您将一个未定义的变量 ( $str) 传递给str_replace函数
,我相信您的意图是将$tr变量传递给 str_replace 函数。

此外,在您的$replace变量中,我已将其更改为在字符之间使用双引号 (")。

<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg"     alt="">
</p>';
$search= '"';
$replace= "'";
$string = str_replace($search,$replace,$tr);
echo $string;
?>

更改$str为未定义的$tr。如果你改变你的双引号$str也会有帮助$replace

  $replace= "'";
于 2012-06-23T02:57:38.260 回答
1

您将文本存储在 $tr 而不是 $str 中。

<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace="'";
$string= str_replace($search,$replace,$tr); // Changed to $tr
echo $string;
?>

并确保转义'in $replace=''';(或使用“”)

于 2012-06-23T02:57:09.967 回答
0

$replace=''';是不正确的。你需要逃避第二个'

使用任一

$replace = "'"这样单引号就不会干扰,或者 $replace = '\''逃脱中间的单引号。

于 2012-06-23T02:58:40.760 回答
0

逃避报价。

$replace='\''; 
// or
$replace="'";
于 2012-06-23T02:59:12.403 回答