Home > Backend Development > PHP Problem > How to add serial number after replacement in php

How to add serial number after replacement in php

PHPz
Release: 2023-04-04 11:42:01
Original
583 people have browsed it

PHP replacement function is a function that performs search and replace operations in a string. They are very useful because in web development, we often need to perform various modifications and operations on strings. This article will focus on how to add serial numbers to strings after using the PHP replacement function.

  1. PHP string replacement function

In PHP, there are three common replacement functions: str_replace(), preg_replace() and str_ireplace().

str_replace(): This function is used to perform simple string replacement in a string.

Syntax:

str_replace(search, replace, subject, [count])
Copy after login

Parameter description:

  • search (required): The string to be searched. Can be a string or array.
  • replace (required): String used for replacement. Can be a string or array.
  • subject (required): The string to be replaced. Can be a string or array.
  • count (optional): Specifies the number of times to be replaced.

Example:

$str = "I love PHP";
echo str_replace("love", "like", $str); // 输出:I like PHP
Copy after login

preg_replace(): This function is used to search and replace strings by regular expressions.

Syntax:

preg_replace(pattern, replacement, subject, [limit], [count])
Copy after login

Parameter description:

  • pattern (required): the regular expression to be searched.
  • replacement (required): String used for replacement. Can be a string or array.
  • subject (required): The string to be replaced. Can be a string or array.
  • limit (optional): Specify the number of times to be replaced. The default is -1, which replaces all matches.
  • count (optional): variable for the number of substitutions.

Example:

$str = "I love PHP, and PHP loves me.";
echo preg_replace("/PHP/", "JavaScript", $str); // 输出:I love JavaScript, and JavaScript loves me.
Copy after login

str_ireplace(): This function is similar to str_replace(), but it is case-insensitive .

Syntax:

str_ireplace(search, replace, subject, [count])
Copy after login

The parameter description is the same as str_replace().

Example:

$str = "I love PHP";
echo str_ireplace("Love", "like", $str); // 输出:I like PHP
Copy after login
  1. Add serial number after replacement

Sometimes, we need to add numerical serial number after replacement to better align the characters The strings are numbered as follows:

$str = "This is a test message. This is also a test message.";
$str_replace = str_replace("test", "sample", $str);
Copy after login

The above code replaces all test in the string $str with sample. However, we would like to add a numerical sequence after the replacement to better distinguish each string.

We can use the preg_replace_callback() function, which can execute the callback function after each replacement. In the callback function, we can get the position of the current match and then add the sequence number based on the position.

Example:

$str = "This is a test message. This is also a test message.";
$count = 1;
$str_replace = preg_replace_callback("/test/", function($matches) use (&$count){
    return "sample". $count++;
}, $str);
Copy after login

The above code uses the preg_replace_callback() function, and uses the callback function after each replacement to replace the string test with sampleAnd add the numerical sequence number. The final output string is:

This is a sample1 message. This is also a sample2 message.
Copy after login

In the callback function, we use an anonymous function to obtain the serial number. The use(&count) syntax allows us to pass the $count variable into an anonymous function using the &count variable so we can update it in the callback function $count variable and use it after each match.

The above is how to use the replacement function in PHP and add a numerical sequence after replacement. I hope readers can better master PHP string processing skills through this article.

The above is the detailed content of How to add serial number after replacement in php. For more information, please follow other related articles on the PHP Chinese website!

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