PHP

Replace a part of the 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.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button