Home > Mobile >  is substr_replace() in php useless? w3schools
is substr_replace() in php useless? w3schools

Time:05-13

As in https://www.w3schools.com/php/func_string_substr_replace.asp

<?php
echo substr_replace("Hello","world",0);
?>

where is the needle parameter? if there is non , so what is the point of this function? when will anybody use it? i just can use use $a = "world" instead of $a = sbustr_replace($whatever,"world",0)

CodePudding user response:

It determines which part of the original string to replace with the replacement based on the start and length arguments.

Since you told it to start at 0 and didn't include a length, it replaces the whole thing.

The function isn't useless, but the way you pass it 0 and nothing as the third and last argument makes it so.

CodePudding user response:

As per the docs (https://www.php.net/manual/en/function.substr-replace.php) this function is used for replacing part of a string when you know the location of the part of the string you want to change. If you want to locate part of the string first, use something like https://www.php.net/manual/en/function.strpos.php or https://www.php.net/manual/en/function.str-replace.php - this is probably must useful for your use-case: you provide a needle (1st argument) and every occurence of this needle in the original string (3rd argument) be replaced with the 2nd argument

  •  Tags:  
  • php
  • Related