Home > Backend Development > PHP Tutorial > Simple usage example of preg_replace_callback function in php

Simple usage example of preg_replace_callback function in php

WBOY
Release: 2016-07-27 16:56:19
Original
1379 people have browsed it

The example in this article describes the usage of preg_replace_callback function in php. Share it with everyone for your reference, as follows:

mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )

The behavior of this function is almost the same as preg_replace(), except that it does not provide a replacement parameter , instead specify a callback function. This function takes as input an array of matches in the target string and returns the string used for replacement.

For example:

preg_replace($skx,$imsz2,$neirong);

Copy after login

For example: There are multiple $skx in $neirong. I need to get a different ID every time I replace it

Example:

<?php
$str='this is a test for this string includes many this';
$replace='/this/x';
$result=preg_replace_callback(
  $replace,
  function($ms){
   static $i;
   $i=$i+1;
   return "that($i)";
  },
  $str
 );
echo $result,"/n";

Copy after login

More interested in PHP related content Readers can check out the special topics on this site: "Summary of PHP Regular Expression Usage", "Summary of PHP Coding and Transcoding Operation Skills", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP Mathematical Operation Skills", "PHP Array (Array) ) Operation Skills Collection", "php String Usage Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "php Common Database Operation Skills Summary"

I hope that this article will be helpful to everyone PHP programming helps.

The above has introduced a simple usage example of the preg_replace_callback function in PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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