PHP program to replace a word in a string

PHP | Replacing a word in a string: Here, we are going to learn how to replace a word in a string using str_replace(), str_ireplace(), and preg_replace() functions?
Submitted by Sayesha Singh, on June 28, 2020

What if a user has typed his name wrong and now he wants to replace his name with the correct name? This a practical scenario, and PHP even has an inbuilt function to do so. There are the following methods to rectify an error made in the string.

1) Using str_replace() function

In this method, str_replace() function replaces all the occurrences of the replaceString by the mentioned string.

The function str_replace() is used for searching a regular expression and replacing a specific content in place of all the occurrences of searched expression. It always returns either a string or an array, according to what was searched and replaced. It has 3 compulsory parameters while one is optional. The compulsory parameters of the function are - value to be searched, the value it will be replaced with and the string in which this value needs to be searched. The optional parameter specifies the count of the number of times the replacement is needed.

Syntax:

str_replace (
    $word_to_replace, 
    $new_word, 
    $string_in_which_word_is_present
    );

Program:

<?php
    $str= 'Sayesaha';
    
    // message to be printed
    $newstr = 'welcome '. $str . ' to learn about PHP language. We are delighted to have you '.$str;
    
    // printing message 
    echo ($newstr."\n");
    
    // changing the name
    $str1= 'Sayesha';
    echo str_replace($str, $str1 , $newstr);
?>

Output:

welcome Sayesaha to learn about PHP language. We are delighted to have you Sayesaha
welcome Sayesha to learn about PHP language. We are delighted to have you Sayesha

2) Using str_ireplace() function

This function does the same thing as str_replace(), i.e. the function str_ireplace() is used for searching a regular expression and replacing a specific content in place of all the occurrences of searched expression, the difference being that str_ireplace() is case insensitive. It is also an inbuilt function of PHP.

Program:

<?php
    $str = 'sayesha';
    
    // message to be printed
    $newstr = 'welcome ' . $str . ' to learn about PHP language. We are delighted to have you ' . $str;
    
    // printing message
    echo ($newstr . "\n");
    
    // changing the name
    $str1 = 'Sayesha';
    echo str_ireplace($str, $str1, $newstr);
?>

Output:

welcome sayesha to learn about PHP language. We are delighted to have you sayesha
welcome Sayesha to learn about PHP language. We are delighted to have you Sayesha

3) Using Search and Replace (preg_replace() function)

What if you need to search a word and then replace it. For example, there was an error while typing an article, and the word 'school' needs to be replaced with the word 'college'.

Syntax:

preg_replace(
    $word_to_replace, 
    $new_word, 
    $string_to_be_searched, 
    $limit, 
    $count
    );

preg_replace() function is used in PHP for searching a regular expression and replacing a specific content in place of the searched expression. It always returns either a string or an array, according to what was searched and replaced.

Program:

<?php
    // string
    $str = 'We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.';
    $s = 'school';
    $newstr = 'college';
    
    // printing message
    echo ('Old message-->' . $str);
    echo ("\n");
    echo ('New message-->');
    
    // Using preg_replace() function
    // to replace the word
    $str = preg_replace('/school/', $newstr, $str);
    
    // Printing the result
    echo $str;
?>

Output:

Old message-->We welcome all of you to our school of PHP.
                This school is one of its kind.
                Many schools dont offer this subject.
New message-->We welcome all of you to our college of PHP.
                This college is one of its kind.
                Many colleges dont offer this subject.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.