Replace text with srt_replace using double quotes

Asked

Viewed 242 times

0

I am trying to use str_replace to change a part of the code,

$mudali = "li id=\"cabeca{$raiz}\" class=\"cabeca\"";
$mudali2 = "li id=\"pes{$raiz}\" class=\"pes\"";

echo $mudali; // resultado li id="cabeca-geral" class="cabeca"
echo $mudali2; // resultado li id="pes-geral" class="pes"

$rodape = str_replace($mudali, $mudali2, $rodape);

But it doesn’t work, but if I take it all down to the double quotes, the code works

$mudali = "cabeca{$raiz}";
$mudali2 = "pes{$raiz}";

echo $mudali; // resultado cabeca-geral
echo $mudali2; // resultado pes-geral

$rodape = str_replace($mudali, $mudali2, $rodape);

Someone knows how to fix?

  • 1

    But what exactly do you want to change? the id? the class? both?

  • I would like to change the id and class, but although echo generates the exact text of what I want to replace, with str_replace will not cost, only works if I double quote

  • You can echo the footer variable and show us also?

  • Have you tried to use single quotes on your variables mudali and mudali2? in this way it will not be necessary to use "" before double quotes.

  • Footer echo is id="<li id="general-head" class="head"><p> ....

  • Vinicius switched to single quotes, still not working, and I had to take the variable from inside the string, changed to $mudali = 'cabeca' . $root . '" class="head"';

  • No way to use a double-quoted string in str_replace?

  • The operation of str_replace is simple, in the first parameter, finds the second, if found, puts the third in the value equal to the second. Rethink how to use this to solve your problem.

  • I can change the text if I change the text without double quotes, for example, only the name of the general head id, but if I look for the text with id="general head' it does nothing

Show 4 more comments

1 answer

1


There’s probably some difference in $rodape which is causing the replacement to fail. Spaces, tabs, accents, something like that. I suggest rewriting all these snippets, where $rodape is created by taking care. To debug the possible difference:

print "HEX1:" . bin2hex($mudali);
print "HEX2:" . bin2hex($rodape);
  • But in the string I have, it has snippets that the code is similar, so it changes there too and I wanted to change only in id

  • In this case your solution is the most correct one. Hence discover what is preventing replace from working. Modifying the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.