Home > Backend Development > PHP Tutorial > How to use str_replace function in php

How to use str_replace function in php

青灯夜游
Release: 2023-04-06 19:16:01
Original
5536 people have browsed it

Usage of str_replace function in php: [str_replace($searchVal, $replaceVal, $subjectVal, $count)]. The str_replace() function is used to replace strings or arrays.

How to use str_replace function in php

str_replace() is a built-in function in PHP, which is used to replace strings or arrays and replace characters in strings or arrays with other characters. A number of characters (case sensitive).

(Related recommendations: php training)

How to use the str_replace function in php?

php str_replace() function is used to perform string replacement operations and is case-sensitive.

Basic syntax:

str_replace ( $searchVal, $replaceVal, $subjectVal, $count )
Copy after login

Parameters: This function accepts four parameters, 3 of which are mandatory and 1 is optional. All these parameters are described below:

● $searchVal: This parameter can be of string and array type. This parameter specifies the string to search and replace. $replaceVal: This parameter can be of string and array type. This parameter specifies the string to be used to replace the

● $searchVal string.

● $subjectVal: This parameter can be of string and array type. This parameter specifies the string or array of strings we want to search for $searchVal and replace with $replaceVal.

● $count: This parameter is optional, if passed, its value will be set to the total number of replacement operations performed on the string $subjectVal.

The function must follow the following rules:

● If the $searchVal and $replaceVal parameters are arrays, then all elements of the $searchVal parameter are in the $subjectVal string Searched for, and replaced by the corresponding element in the $replaceVal parameter.

● If the number of elements in $replaceVal is less than the number of elements in the $searchVal array, then if other elements of the $searchVal parameter in the $subjectVal parameter are present, they will be replaced by the empty string.

● If the $subjectVal parameter is also an array rather than a string, then all elements of $subjectVal will be searched.

Return value: This function returns a string or array based on the $subjectVal parameter with a replacement value.

Let’s take a look at how to use the php str_replace() function through an example.

Example 1:

<?php
echo str_replace("world","php.cn","Hello world!");
?>
Copy after login

Output:

Hello php.cn!
Copy after login

Example 2:

<?php
$i = "i like JavaScript";
echo str_replace("JavaScript","php",$i);
?>
Copy after login

Output:

i like php
Copy after login

The above is the detailed content of How to use str_replace function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template