Implementation of preg_replace in php in jsreplace c#

WBOY
Release: 2016-07-29 08:41:35
Original
1082 people have browsed it

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 results
print preg_replace($patterns,
$replacements, $string);
?>


The output results are 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;
}

The above introduces the implementation of preg_replace in php in jsreplace c#, including the content of jsreplace. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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