Implementation of php in c # preg_replace_PHP tutorial-php Tutorial

Source: Internet
Author: User
Implement preg_replace in php in c. Rewrite phppreg_replace with c. The PHP language is very powerful, mainly supported by its powerful functions. In this article, we will explain in detail how to rewrite php preg_replace with c.
The PHP language is very powerful, mainly supported by its powerful functions. In this article, we will explain in detail how to use the PHP function preg_replace.
PHP function preg_replace () prototype: mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit])
The PHP function preg_replace () is more powerful than c. Arrays can be used for the first three parameters. The fourth parameter $ limit can be used to set the number of replicas. by default, all replicas are replaced. Code 6.7 is an array replacement application instance.
PHP function preg_replace () code 6.7 array replacement

The code is as follows:


<? Php
// String
$ String = "Name: {Name} <br> \ nEmail:
{Email} <br> \ nAddress: {Address} <br> \ n ";
// Mode
$ Patterns = array (
"/{Address }/",
"/{Name }/",
"/{Email }/"
);
// Replace the string
$ Replacements = array (
"No. 5, Wilson St., New York, U. S. ",
"Thomas Ching ",
"Tom@emailaddress.com ",
);
// Output mode replacement result
Print preg_replace ($ patterns,
$ Replacements, $ string );
?>


The output result is as follows.
Name: Thomas Ching ",
Email: tom@emailaddress.com
Address: No. 5, Wilson St., New York, U. S.
C #

The code is as follows:


Public static String PregReplace (this String input, string [] pattern, string [] replacements ){
If (replacements. Length! = Pattern. Length) throw new ArgumentException ("Replacement and Pattern Arrays must be balanced ");
For (var I = 0; I <pattern. Length; I ++)
{Input = Regex. Replace (input, pattern [I], replacements [I]);}
Return input;
}

Preg_replace is rewritten in c. The PHP language is very powerful, mainly supported by its powerful functions. We will give you a detailed explanation in this article...

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.