Home  >  Article  >  Backend Development  >  How to implement data replacement in php

How to implement data replacement in php

藏色散人
藏色散人Original
2021-02-25 10:01:102827browse

How to implement data replacement in php: First create a PHP sample file; then use the "str_replace" function to replace a string with a string or replace an array with an array.

How to implement data replacement in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

There are four types of PHP string replacement str_replace() functions Usage

// 参数 $search,要替换的字符串,或数组
 
// 参数 $replace,被用来替换的字符串或数组
 
// 参数 $subject,被查询的字符串或数组
 
// 参数 $count,可选,如果被指定,将为设置为替换的次数
 
// 返回值:该函数返回替换后的数组或者字符串(新生成的)
 
 
  //实例一:字符串替换字符串
  $str1 = str_replace("red","black","red green yellow pink purple");
  echo $str1."";  //输出结果为black green yellow pink purple
 
  //实例二:字符串替换数组键值
  $arr = array("blue","red","green","yellow");
  $str1 = str_replace("red","pink",$arr,$i);
  print_r($str1);
 
  //实例三:数组替换数组,映射替换
  $arr1 = array("banana","orange");
  $arr2 = array("pitaya","tomato");
  $con_arr = array("apple","orange","banana","grape");
  $con_rep = str_replace($arr1,$arr2,$con_arr,$count);
  print_r($con_rep);
 
  //实例四:如$search为数组,$replace为字符串时
  $search = array("banana","grape");
  $replace = "tomato";
  $arr = array("banana","apple","orange","grape");
  $new_arr = str_replace($search,$replace,$arr,$count);
  print_r($new_arr);

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to implement data replacement in php. 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