LoginSignup
3
3

More than 5 years have passed since last update.

PHP7調査(30)preg_replace_callback_array関数の新設

Last updated at Posted at 2015-05-12

preg_replace_callback_array()という新しい関数がPHP7から実装されました。要は配列を受け取れるバージョンのpreg_replace_callback()です。

<?php
$str = 'aBcDeF';
$str = preg_replace_callback_array([
        '/[ace]/' => function ($m) { return strtoupper($m[0]); },
        '/[BDF]/' => function ($m) { return strtolower($m[0]); },
    ], $str);
var_dump($str); // string(6) "AbCdEf"

preg_replace()は複数組の置換を一気に書けるのにpreg_replace_callback()だと書けないという問題意識から作られた関数のようです。でも、そんなに必要かなあ…?

参照

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3