What is difference between str_replace and substr_replace



What is difference between str_replace and substr_replace..

Answer / 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

More PHP Interview Questions

What is the name of the scripting engine that powers PHP?

1 Answers  


what is output of echo 1< 2 and echo 1 >2

8 Answers   HCL, Navigators Software,


What is warning – “cannot modify header information – headers already sent”?

1 Answers  


What is the difference between client-side and server-side programming?

1 Answers  


What is use of isset function in php?

1 Answers  


List some features of php that are deprecated in php

1 Answers  


What is helper function?

1 Answers  


List types of array are available in php?

1 Answers  


What are the methods to submit form in php?

1 Answers  


What are different types of runtime errors in php?

1 Answers  


Is php fully object oriented?

1 Answers  


How to remove blank spaces from the string?

1 Answers  


Categories