preg_match一些问题
Posted init-007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了preg_match一些问题相关的知识,希望对你有一定的参考价值。
<?php
$string = ‘The quick brown fox jumps over the lazy dog.‘;
$patterns = array();
$patterns[0] = ‘/quick/‘;
$patterns[1] = ‘/brown/‘;
$patterns[2] = ‘/fox/‘;
$replacements = array();
$replacements[2] = ‘bear‘;
$replacements[1] = ‘black‘;
$replacements[0] = ‘slow‘;
echo preg_replace($patterns, $replacements, $string);
?>
以上例程会输出:
The bear black slow jumps over the lazy dog.
对模式和替换内容按key进行排序我们可以得到期望的结果。
<?php
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>
以上例程会输出:
The slow black bear jumps over the lazy dog.
==============================================================
<?php
$patterns = array (‘/(19|20)(d{2})-(d{1,2})-(d{1,2})/‘,
‘/^s*{(w+)}s*=/‘);
$replace = array (‘3/4/12‘, ‘$1 =‘);
echo preg_replace($patterns, $replace, ‘{startDate} = 1999-5-27‘);
?>
以上例程会输出:
$startDate = 5/27/1999
以上是关于preg_match一些问题的主要内容,如果未能解决你的问题,请参考以下文章