Home  >  Article  >  Backend Development  >  Detailed explanation of str_replace() in php (with code examples)

Detailed explanation of str_replace() in php (with code examples)

autoload
autoloadOriginal
2021-04-30 10:35:332590browse

Detailed explanation of str_replace() in php (with code examples)

Strings are a commonly used type in php. Replacement of strings is not as convenient as arrays. So how to replace characters in a string? This article will explain Let’s take a look at how to use the str_repalce() function. First, let’s take a look at the syntax of the function:

str_replace    ( mixed $search   , mixed $replace   , mixed $subject   , int $count = ?   )
  • $search: search Target value, multiple targets can be specified.

  • #$replace: the replacement value of search.

  • $subject: Array or string to perform replacement

  • $count: Optional, if specified, its value will be set is the number of times replacement occurs.

  • Return value: This function returns the replaced array or string.

Code example:

1. There are three required parameters

<?php
$str="ok is great,that is php.cn";
$str1=str_replace("is","hehe",$str);
print_r($str1);
?>
输出:ok hehe great,that hehe php.cn

2. There are four parameters

<?php

$str="ok is great,that is php.cn";
$str1=str_replace("is","hehe",$str,$count);
print_r($str1);
echo "<br>"."总共替换了:".$count;
?>
输出:ok hehe great,that hehe php.cn
总共替换了:2

Recommended: 2021 PHP Summary of interview questions (collection)》《php video tutorial

The above is the detailed content of Detailed explanation of str_replace() in php (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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