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

autoload
Release: 2023-03-09 12:48:01
Original
2614 people have browsed it

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 = ?   )
Copy after login
  • $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);
?>
Copy after login
输出:ok hehe great,that hehe php.cn
Copy after login

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;
?>
Copy after login
输出:ok hehe great,that hehe php.cn
总共替换了:2
Copy after login

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!

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