PHP program to replace a word in a string

Given a string. Learn, how to replace a word with another in it?
Submitted by Sayesha Singh, on June 28, 2020 [Last updated : March 13, 2023]

PHP - Replacing Word in a String

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.

The following functions can be used to replace a string's word with another,

  1. Using str_replace() function
  2. Using str_ireplace() function
  3. Using Search and Replace (preg_replace() function)

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
    );

PHP code to replace a word in a string using str_replace()

<?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.

PHP code to replace a word in a string using str_ireplace()

<?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.

PHP code to replace a word in a string using search and replace (preg_replace() function)

<?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.

PHP String Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.