String Function in php programming language we will see string replace with help of string function.
substr_replace();
Now substr_replace function takes four arguments first argument we take is the string we want to look out second argument is the string that we want to replace the third argument is where we want to start and fourth argument is where we want to end how many characters or length of the replacement. substr function which will return the specific part of the string so we can specify like example we had a $string = alex; a comes 0 l comes 1 and if we want e and x which are at position 2 and 3 we can specify with substr function. str_replace function will find string within string and it will replace it with the specified string therefore substr_replace function is combination of two function such as substr and str_replace functions. Why we need substr_replace function ? we need this because we searching for something and replacing by specific amount of data. So, for example if you want to copy string in half this would be function to use.Code for string substr_replace function (Screenshot1):-
Screenshot1 |
<?php $string = 'This part don\t search. This part search.'; $string_new = substr_replace($string, 'alex', 29,4); echo $string_new; ?>
Output (Screenshot2):-
Screenshot2 |
In this example we take string in variable like $string. using substr_replace function which had four argument here first the string then the word replaced is alex and in string which position the word is there which we want to replace which is on 29 position part and how many word you want to replace so 4 word is our fourth arguments and as you can see the output of the example part word had been changed to alex.
Now why it will be useful and where it will be useful in real life example ?
Let say we have a form where user word entered data like word processes you can search and replace the word from the data we already seen searching word from string expression match and can use string length function then you can take user input and specify what you like to replace
No comments:
Post a Comment
Thanks For Comment Will get you Soon..