How to replace a text in a string with another text using PHP ?
Last Updated :
18 Jul, 2024
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()
This 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:
- $oldString: It specifies the string to be searched and replaced.
- $newString: It specifies the string that we want to replace with the $oldString string.
- $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 geeksforgeeks";
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 geeksforgeeks
Modified String : Hi! That is geeksforgeeks
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 geeksforgeeks";
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 geeksforgeeks
Modified String : Hi!__This__is__geeksforgeeks
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 geeksforgeeks";
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 geeksforgeeks
Modified String : Hi! This is GFG
Approach 3: Using preg_replace()
Using preg_replace() in PHP, replace text in a string based on a regular expression pattern. It allows for flexible and precise replacements, making it suitable for complex matching and substitutions.
Example:
PHP
<?php
$string = "Hello, World!";
$newString = preg_replace('/World/', 'PHP', $string);
echo $newString;
?>
Approach 4: Using substr_replace() method
The substr_replace() method is used to replace a part of a string with another string. This function is useful when you know the position and length of the substring you want to replace.
Example: In this example, we will use the substr_replace method.
PHP
<?php
// Declaring a string variable
$str = "Hello, World!";
echo("Original String : ");
echo($str . "\n");
// Replacement string
$replacement = "PHP";
// Starting position
$start = 7;
// Length of the substring to be replaced
$length = 5;
// Replacing part of string
$final_str = substr_replace($str, $replacement, $start, $length);
echo("Modified String : ");
echo($final_str);
?>
OutputOriginal String : Hello, World!
Modified String : Hello, PHP!
Approach 5: Using strtr() Method
The strtr() function in PHP is another useful method to perform string replacements. This function can replace multiple characters or substrings at once based on a mapping of replacements. It is efficient when dealing with a large number of replacements.
Example:
PHP
<?php
$originalString = "Hello World!";
$modifiedString = strtr($originalString, "eo", "ie");
echo "Original String: " . $originalString . "\n";
echo "Modified String: " . $modifiedString;
?>
OutputOriginal String: Hello World!
Modified String: Hille Werld!
Similar Reads
How to replace a string by another string in AngularJS ?
In this article, we will see how to select a string from the text and replace it with another string using AngularJS and will understand it through the illustrations. The replace() method can be used to search the particular character(s), which is getting replaced with a specific character(s) &
2 min read
How to replace all dots in a string using JavaScript ?
We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll()
4 min read
How to replace a portion of strings with another value in JavaScript ?
In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca
3 min read
Change the tag's contents and replace with the given string using BeautifulSoup
Prerequisites: Beautifulsoup Beautifulsoup is a Python library used for web scraping. This powerful python tool can also be used to modify html webpages. This article depicts how beautifulsoup can be employed to change contents within a tag and replace the contents to be changed with the given strin
1 min read
How to replace String in PHP ?
Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
2 min read
How to replace multiple characters in a string in PHP ?
A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla
3 min read
How to replace all occurrences of a string using Vue.js filters ?
In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation
2 min read
How to UPDATE and REPLACE Part of a String in PL/SQL?
PL/SQL is a procedural language designed to enable developers to combine the power of procedural language with Oracle SQL. It is developed by Oracle and serves as one of the three key programming languages embedded in the Oracle database, alongside SQL and Java. PL/SQL includes procedural language e
3 min read
How to Display HTML Tags as Plain Text using PHP?
HTML tags begin with the less-than character and end with greater-than character, the text inside the tag formatted and presented according to the tag used. Every tag has special meaning to the browser but there are cases when to show plain HTML code in webpage. There are various methods in PHP to s
3 min read
How to display string values within a table using PHP ?
A table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. Tables are useful for various tasks such as presenting text information and numerical data.Tables can be used to compare two or more i
1 min read