Home  >  Article  >  Backend Development  >  Tutorial on implementing preg_replace_PHP in php in c#

Tutorial on implementing preg_replace_PHP in php in c#

WBOY
WBOYOriginal
2016-07-21 15:41:42948browse

Rewrote php preg_replace in 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])
PHP function preg_replace() is more powerful than c. The first three parameters can all use arrays; the fourth parameter $limit can set the number of replacements, and the default is to replace all. Code 6.7 is an application example of array replacement.
PHP function preg_replace() code 6.7 Array replacement

Copy code The code is as follows:

< ?php
//String
$string = "Name: {Name}< br>nEmail:
{Email}< br>nAddress: {Address}< br>n";
// Pattern
$patterns =array(
"/{Address}/",
"/{Name}/",
"/{Email}/"
);
/ /Replacement string
$replacements = array (
"No.5, Wilson St., New York, U.S.A",
"Thomas Ching",
"tom@emailaddress.com",
);
//Output pattern 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.A
c#
Copy code 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321099.htmlTechArticleRewrote php preg_replace in c#. The PHP language is very powerful, mainly supported by its powerful functions. In this article we will explain to you in detail...
Statement:
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