Directly run the code {code...}. Shouldn't the result be 0Handler1TestHandler. php? Is there a problem? Run the Code directly.
$str = '\Handler\TestHandler.php';$need = ['\\', '\\'];$new_need = [0,1];$result = str_replace($need, $new_need, $str);//$result=>0Handler0TestHandler.php
The result should not be0Handler1TestHandler.php?
Is there a problem?
Reply content:
Run the Code directly.
$str = '\Handler\TestHandler.php';$need = ['\\', '\\'];$new_need = [0,1];$result = str_replace($need, $new_need, $str);//$result=>0Handler0TestHandler.php
The result should not be0Handler1TestHandler.php?
Is there a problem?
Your two\All are replaced by 0. Syntax details
Php should not have this built-in method. You need to write a function by yourself. The following should be able to implement your function.
function replace ($str) { $i = 0; $result = ''; $arr = explode("\\", $str); foreach ($arr as $val) { if ($val === '') continue; $result .= ($i++).$val; } return $result;}
Because array replacement is a successive replacement, the first replacement has already replaced two\If it is replaced with 0, the replacement will not match.\No.