php str_replace类固醇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php str_replace类固醇相关的知识,希望对你有一定的参考价值。

<?php

namespace NorthernLights\Util;

/**
 * Map/replace string accordingly to supplied character map
 * Note:
 *       There is certain overhead because we use 4 function calls main -> mapper -> array_keys -> array_values
 *       versus that it can be done with just single str_replace()
 *       However, accordingly to benchmarks, difference becomes noticeable only after 1 000 000 iterations (~300ms slower)
 *
 * @param string $subject
 * @param array $replMap
 *   - Schema: "target" => "replacement"
 *
 * @param callable|null $mapper
 *   - Mapper function. Must accept arguments array, array, string and return string
 *   - Default mapper function is str_replace
 *
 * @author Aleksandar Puharic <xzero@elite7hackers.net>
 *
 * @return string
 */
function strMap($subject, array $replMap, callable $mapper = null)
{
    // Return immediately if empty
    if ($subject === '') {
        return '';
    }

    // Return immediately if empty
    if ($replMap === []) {
        return $subject;
    }

    $mapper = ($mapper === null) ? 'str_replace' : $mapper;

    return $mapper(
        array_keys($replMap),
        array_values($replMap),
        $subject
    );
}

以上是关于php str_replace类固醇的主要内容,如果未能解决你的问题,请参考以下文章

utf-8 (PHP, str_replace) 中是不是有不同类型的双引号?

PHP str_replace 函数

关于php strtr 和 str_replace 效率的问题

PHP str_replace()字符串匹配

php中的字符串常用函数 str_replace 字符串替换

PHP:“......变量可以通过引用传递”在str_replace()?