How to Replace Part of a string with Another String in PHP? Last Updated : 08 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given a String, the task is to replace the part of a string with another string in PHP. There are several methods to replace part of a string with another string. This operation is useful when you need to modify a string, such as replacing certain characters or substrings. Table of ContentUsing str_replace() FunctionUsing substr_replace() FunctionUsing Regular ExpressionsUsing strtr() FunctionApproach 1: Using str_replace() FunctionThe str_replace() function is a built-in PHP function that replaces all occurrences of a search string with a replacement string in a given string. PHP <?php $str = "Welcome to GeeksforGeeks - Computer Science Portal"; $newStr = str_replace("GeeksforGeeks - Computer Science Porta", "GeeksforGeeks", $str); echo "New String: $newStr"; ?> OutputNew String: Welcome to GeeksforGeekslExplanation:The str_replace() function takes three arguments: the search string, the replacement string, and the input string. It replaces all occurrences of the search string with the replacement string in the input string.Approach 2: Using substr_replace() FunctionThe substr_replace() function replaces a portion of a string with another string. PHP <?php $str = "Welcome to GeeksforGeeks - Computer Science Portal"; $newStr = substr_replace($str, "GeeksforGeeks", 11, 50); echo "New String: $newStr"; ?> OutputNew String: Welcome to GeeksforGeeksExplanation:The substr_replace() function takes four arguments: the input string, the replacement string, the start position of the replacement, and the length of the portion to be replaced.Approach 3: Using Regular ExpressionsRegular expressions can also be used to replace part of a string with another string. PHP <?php $str = "Welcome to GeeksforGeeks - Computer Science Portal"; $newStr = preg_replace("/GeeksforGeeks - Computer Science Portal/", "GeeksforGeeks", $str); echo "New String: $newStr"; ?> OutputNew String: Welcome to GeeksforGeeksExplanation:The preg_replace() function performs a regular expression search and replace. It takes three arguments: the regular expression pattern, the replacement string, and the input string.Approach 4: Using strtr() FunctionThe strtr() function in PHP is used to translate certain characters or replace substrings in a string. It can replace multiple characters or substrings simultaneously.Example: In this example, the strtr() function takes two arguments: the input string and an associative array where the keys are the substrings to be replaced and the values are the corresponding replacements. PHP <?php // Sample string $string = "Hello, world!"; // Define the replacements $replacements = array("Hello" => "Hi", "world" => "earth"); // Replace parts of the string $result = strtr($string, $replacements); echo $result; ?> OutputHi, earth! Comment More infoAdvertise with us Next Article How to Replace Part of a string with Another String in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-string Similar Reads 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 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. Table of ContentUsing str_replace() method - The str_replace() Using str_irepla 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 How to UPDATE and REPLACE Part of a String in SQL Server In SQLServer, efficient manipulation of strings is crucial for managing databases effectively. Among the fundamental operations are updating and replacing parts of strings within tables. These operations are invaluable for correcting data inconsistencies, enhancing data quality, and transforming tex 4 min read How to UPDATE and REPLACE Part of a String in MariaDB MariaDB is one of the most popular open-source database systems. It is developed by the developers of MySQL. In this article, we will How to UPDATE and REPLACE part of a string in MariaDB along with various examples and methods and so on. MariaDB REPLACE String FunctionThe REPLACE function is used t 3 min read How to Replace Part of a String in MySQL? To replace a part of a string in MySQL we use the REPLACE function. MySQL provides this method to manipulate string data in the tables. In this article, we are going to see how we can update a part of the string with some other value in MySQL. Understanding this can enable string manipulation in a m 4 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 read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 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 remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re 3 min read Like