首页 > 其他 > 详细

preg_match一些问题

时间:2018-08-12 12:58:35      阅读:165      评论:0      收藏:0      [点我收藏+]
<?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/\1\2‘, ‘$\1 =‘);
echo preg_replace($patterns, $replace, ‘{startDate} = 1999-5-27‘);
?>

以上例程会输出:

$startDate = 5/27/1999

preg_match一些问题

原文:https://www.cnblogs.com/init-007/p/9462272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!