Simple usage example of the preg_replace_callback function in php, pregreplacecallback

Source: Internet
Author: User

Simple usage example of the preg_replace_callback function in php, pregreplacecallback

This example describes the usage of the preg_replace_callback function in php. We will share this with you for your reference. The details are as follows:

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

The behavior of this function is almost the same as that of preg_replace (), except that a replacement parameter is provided, but a callback function is specified. This function uses the matching array in the target string as the input parameter and returns the string to be replaced.

For example:

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

For example, there are multiple $ skx in $ neirong. I can get a different ID for each replacement.

Example:

<? Php $ str = 'this is a test for this string between des against this '; $ replace ='/this/X'; $ result = preg_replace_callback ($ replace, function ($ ms) {static $ I; $ I = $ I + 1; return "that ($ I)" ;}, $ str); echo $ result, "/n ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.