zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    c++ static_cast和dynamic_cast详解
    python 字符串格式化 format
    python zip() 函数
    零零散散的python笔记 2
    PAT 1017
    PAT 1016
    PAT 1015
    Jordan Lecture Note-8: The Sequential Minimal Optimization Algorithm (SMO).
    Jordan Lecture Note-7: Soft Margin SVM
    PAT 1014
  • 原文地址:https://www.cnblogs.com/init-007/p/9462272.html
Copyright © 2011-2022 走看看