Replace a part of the string in PHP

replace a part of string in php

In this blog we will see how we can replace a part of the string, sometimes we have to change some string parts let’s suppose you have string name as free_shipping_title and you want to make it as free_shipping_status.

So we will how we can do this stuff in PHP in a very easy manner. To replace a part of the string we will use the PHP str_replace function.

Defination and usage

The str_replace() function replaces some characters with some other characters in a string.

This function works by the following rules:

  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, find and replace is performed with every array element
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value

Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary-safe.

Example

<?php
echo str_replace("title","status","free_shipping_title");
?>

Output:

free_shipping_status

I hope you like this blog, please rate me 5 if I resolved your issue.

0 0 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x