duankuang1046 2014-03-17 12:48
浏览 35
已采纳

str_replace问题,PHP 5.2中的字符“<”和“>”

I use PHP 5.2 When I omit the special characters ">" and "<" then this code works :

<?php
$old = 'stroke-dasharray="-1"';
$new = 'stroke-dasharray="none"';
$ligne = '<path stroke-opacity="1" stroke-miterlimit="1" stroke-linejoin="round" stroke-linecap="round" stroke-dasharray="-1" stroke-width="1" fill-opacity="0" style="fill-opacity: 0; stroke-width: 1; stroke-linecap: round; stroke-linejoin: round; stroke-miterlimit: 1; stroke-opacity: 1; cursor: move;" d="M179.5,80.5L317.5,80.5" stroke="#808080" fill="none"/>';
$ligne = htmlentities($ligne);
$chaine = str_replace($old, $new, $ligne);
$chaine = html_entity_decode($chaine);
echo "remplacement donne : ".$chaine;
?>

So how to code correctly the str_replace ?

  • 写回答

2条回答 默认 最新

  • duanfu4446 2014-03-17 12:52
    关注

    Change your code to this:

    <?php
        $old = 'stroke-dasharray="-1"';
        $new = 'stroke-dasharray="none"';
        $ligne = '<path stroke-opacity="1" stroke-miterlimit="1" stroke-linejoin="round" stroke-    linecap="round" stroke-dasharray="-1" stroke-width="1" fill-opacity="0" style="fill-opacity: 0; stroke-width: 1; stroke-linecap: round; stroke-linejoin: round; stroke-miterlimit: 1; stroke-opacity: 1; cursor: move;" d="M179.5,80.5L317.5,80.5" stroke="#808080" fill="none"/>';
        //$ligne = htmlentities($ligne);
        $chaine = str_replace($old, $new, $ligne);
        //$chaine = html_entity_decode($chaine);
        echo "remplacement donne : ".$chaine;
    ?>
    

    When you use htmlentities your replacement string $old will not be found in $ligne hence no replacement will take place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TLE9879QXA40 电机驱动
  • ¥15 clion的参数提示怎么关闭
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀
  • ¥15 mifare plus卡认证
  • ¥30 LSTM预测结果是一条直线