本文整理汇总了PHP中Regex::MultiSubstrReplace方法的典型用法代码示例。如果您正苦于以下问题:PHP Regex::MultiSubstrReplace方法的具体用法?PHP Regex::MultiSubstrReplace怎么用?PHP Regex::MultiSubstrReplace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Regex
的用法示例。
在下文中一共展示了Regex::MultiSubstrReplace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: RenumberNamedCaptures
/**
*
* Gives a unique id to each named capture within a regex.
*
* Reassigns unique identifiers to named captures within a regular expression. The new
* identifiers will have the form "prefix_x", where "prefix" is given by the $prefix
* parameter, and "x" a unique identifier starting from 0.
*
* @param string $pattern
* Regex pattern containing potential named captures to be renamed.
*
* @param array $correspondances
* On output, will hold an associative array whose keys are the new capture group names, and values the old ones.
*
* @param string $prefix
* Prefix string for capture name replacements.
*
* @return string
* Returns the input pattern with all named captures replaced by unique identifiers.
*
* @notes
* This method, along with the GroupNamedCaptures() one, is used by the Preg*Ex methods
* to allow processing of regular expressions having duplicate named captures.
*
*/
public static function RenumberNamedCaptures($pattern, &$correspondances = [], $prefix = 'match_')
{
static $re = '/
\\( \\? P <
(?P<pattern> [^>]+ )
>
/imsx';
// Get named captures
if (self::PregMatchAll($re, $pattern, $matches, PREG_OFFSET_CAPTURE)) {
$index = 0;
$pattern_matches = [];
// Loop through pattern matches
foreach ($matches['pattern'] as $match) {
$pname = $match[0];
$poffset = $match[1];
$newpattern = "{$prefix}{$index}";
// Build the correspondance array
$correspondances[$newpattern] = $pname;
// Add this entry (old name, new name, offset) into an array for the Regex::MultiSubsrReplace() method
$pattern_matches[] = [$pname, $newpattern, $poffset];
$index++;
}
// Perform the multiple-string replace
$new_pattern = Regex::MultiSubstrReplace($pattern, $pattern_matches);
} else {
$new_pattern = $pattern;
}
// All done, return
return $new_pattern;
}