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...