pheromix
pheromix

Reputation: 19317

str_replace issue with the characters "<" and ">" in 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 ?

Upvotes: 0

Views: 123

Answers (2)

user3395757
user3395757

Reputation:

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.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

htmlentities encodes " to &quot;, so your str_replace won't find it.

You should not be using htmlentities here. Remove that and the html_entity_decode.

Upvotes: 1

Related Questions