Code has been added to clipboard!

PHP str_replace()

Reading time 2 min
Published Aug 14, 2017
Updated Sep 24, 2019

PHP str_replace: Main Tips

  • By using this function, you can replace characters in a text string.
  • PHP str_replace is case-sensitive. If you need to perform a case-insensitive search, try str_ireplace() function.
  • If the variable specified by the third argument is an array, the function will check every array element and will return an array.
  • If the first and second function arguments are both arrays, and the second argument has fewer elements than the first, an empty string will be used as a replacement.
  • If the first argument is an array and the second is a string, the string will be used for every array element.

Function Explained

By using the str_replace PHP function, you will replace a string specified in the first argument with a string specified in the second argument. The third argument is a string or an array in which search and replacement will take place. PHP str_replace returns a string or an array with the replaced values.

Let's see a simple example:

Example
<?php
  echo str_replace("world","Peter","Hello world!"); 
?>

Correct Syntax

Here we have is a simple scheme for the correct syntax of the PHP string replace function:

str_replace(find, replace, string, count)

In the table below, each parameter of the function is described separately for you to get a better idea:

Parameter Description
find Necessary. Defines the value that has to be found.
replace Necessary. Defines the replacement value.
string Necessary. Specifies a string where the search will take place.
count Not necessary. Sets a variable that counts the changes made.

Code Examples

In the example you can see below, PHP string replace function finds the word cat in the $arr array and replaces it with the word lama. We only have one change done in this case, but the function would successfully replace all the words cat if the code would consist of more than one.

Variable $i holds a value of how many replacements did the function do:

Example
<?php
  $arr = array("Best", "pet", "is", "cat");
  print_r(str_replace("cat", "lama", $arr, $i));
  echo "Replace count: $i"; 
?>

In the example below, PHP str_replace function replaces two strings in the $arr array with the letter B. See for yourself:

Example
<?php
  $find = array("Hello", "world");
  $replace = "B";
  $arr = array("Hello", "world", "!");
  print_r(str_replace($find, $replace, $arr));
?>
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()