简体   繁体   中英

PHP str_replace special chars

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!

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); <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM