What is difference between str_replace and substr_replace

Answer Posted / shivvshann

Difference between str_replace and substr_replace

str_replace —
Replace all occurrences of the search string with the
replacement string.

Example for str_replace()

<?php
echo str_replace("world","siva","Hello world!");
?>

substr_replace -

The function substr_replace introduces some additional
functionality to compliment str_replace. substr_replace is a
more mathematically based replace function, which relies on
starting points and lengths to replace parts of strings, as
opposed to searching and replacing.

Example for substr_replace()

//string that needs to be customized
$original = "ABC123 Hello Mr. siva! DEF321";

//starting point 5
$sp5 = substr_replace($original, "Five", 5);
//starting point 12
$sp12 = substr_replace($original, "Twelve", 12);
//starting point 0
$sp0 = substr_replace($original, "Zero", 0);
//starting point -1
$spneg1 = substr_replace($original, "Negative 1", -1);

//Echo each string
echo "Original String: $original <br />";
echo "Starting Point 5: $sp5 <br />";
echo "Starting Point 12: $sp12 <br />";
echo "Starting Point 0: $sp0 <br />";
echo "Starting Point -1: $spneg1 ";

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to call a php function from html button?

607


What is return value in php?

512


What language is php written in?

577


Does php 5 support exceptions?

675


How to return a value back to the function caller?

555






Explain about PHP filter and why it should be used?

553


What does a delimiter do in mysql?

526


Which function Returns the time of sunrise for a given day / location in PHP.

582


What are the differences between php3 and php4 and php5? What is the current stable version of php? What advance thing in php7?

615


Tell me how do I escape data before storing it into the database?

521


What is static in php?

529


Write a program in php to check whether a number is prime or not?

494


Why do we use cookie?

540


How to remove html tags from data in php?

622


How to list all values of submitted fields?

543