当前位置: 首页>>代码示例>>PHP>>正文


PHP Regex::MultiSubstrReplace方法代码示例

本文整理汇总了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;
    }
开发者ID:wuthering-bytes,项目名称:phpclasses-org,代码行数:55,代码来源:Regex.class.php


注:本文中的Regex::MultiSubstrReplace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。