Thursday, June 27, 2024
HomeLanguagesPhpHow to replace a text in a string with another text using...

How to replace a text in a string with another text using PHP ?

A string is a sequence of one or more characters. A string is composed of characters, each of which can be replaced easily in the script. A large number of in-built PHP methods can be used to perform string replacements. 

Approach 1: Using str_replace() method – The str_replace() method is used to replace a text in the string with another string. However, the method compares the case of the mentioned strings. The string remains unmodified in case the old string is not found with any matchings. 

str_replace($oldString, $newString, $string)

Parameters: 

  1. $oldString: It specifies the string to be searched and replaced.
  2. $newString: It specifies the string that we want to replace with the $oldString string.
  3. $string: It specifies the string or array of strings which we want to search for $oldString and replace with $newString.

Example:

PHP




<?php
  
// Declaring a string variable
$str = "Hi! This is neveropen";
echo("Original String : ");
echo($str."<br>");
  
// Old string
$str_old = "This";
  
// New string
$str_new = "That";
  
// Replacing part of string
$final_str = str_replace($str_old,$str_new,$str);
  
echo("Modified String : ");
echo($final_str);
  
?>


 

Output:

Original String : Hi! This is neveropen
Modified String : Hi! That is neveropen

Note: If a part of the string is found at multiple portions, each instance of the old string is replaced with the new counterpart. The following code snippet illustrates the replacement of the symbol ” ” with an underscore. 

PHP




<?php
  
// Declaring a string variable
$str = "Hi! This is neveropen";
echo("Original String : ");
echo($str . "\n");
  
// Old string
$str_old = " ";
  
// New string
$str_new = "__";
  
// Replacing part of string
$final_str = str_ireplace($str_old,$str_new,$str);
  
echo("Modified String : ");
echo($final_str);
  
?>


Output:

Original String : Hi! This is neveropen
Modified String : Hi!__This__is__neveropen

Approach 2: Using str_ireplace() method – In case, we wish to replace the string irrespective of the case in which the old string is in, the str_ireplace() method is used. The method is supported in PHP 5+. The string behaves in a similar manner as compared to the str_replace() method. 

Syntax:

str_ireplace($oldString, $newString, $string)

Parameters:

  • $oldString: The string to locate in the string.
  • $newString: The string to replace the old string with.
  • $string: The specified string.

PHP




<?php
  
// Declaring a string variable
$str = "Hi! This is neveropen";
  
echo("Original String : ");
echo($str . "<br>");
  
// Old string
$str_old = "GEEKSFoRGEEks";
  
// New string
$str_new = "GFG";
  
// Replacing part of string
$final_str = str_ireplace($str_old,$str_new,$str);
  
echo("Modified String : ");
echo($final_str);
  
?>


Output:

Original String : Hi! This is neveropen
Modified String : Hi! This is GFG

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments