-1

im trying to replace two ?? chars inside an HTML file as follows:

<?php
$file="test.html";

$q1 = "??";
$q2="?";

$string=file_get_contents($file);

$string=substr_replace("$q1", "$q2",$string);
file_put_contents($file, $string);
?>

but im afraid its not working. it works fine with normal text.

any ideas? thanks in advance!

  • 1
    What do you mean by it's not working? – Spoody Oct 22 '17 at 11:48
  • its not replacing the two ?? simbols , it should change to just one ? – user2965840 Oct 22 '17 at 12:19
  • [`substr_replace()`](http://php.net/manual/en/function.substr-replace.php) **is not** [`str_replace()`](http://php.net/manual/en/function.str-replace.php). You call `substr_replace()` but you pass it the arguments expected by `str_replace()`. Of course it doesn't work. – axiac Oct 22 '17 at 16:33

2 Answers2

0

You are using the parameter set for a str_replace() but in a substr_replace() call.

Try

$string=str_replace($q1, $q2,$string);

The manual substr_replace()

The manual str_replace()

Can I suggest that you add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

finally i found a solution, it works like this:

$q1 = "??";
$q2="?";

$cadena=utf8_encode(file_get_contents($nombrearchivo));
$cadena = html_entity_decode($cadena, ENT_QUOTES);
$cadena=str_replace("$q1", "$q2",utf8_encode($cadena));
file_put_contents($nombrearchivo, $cadena);