dongtu0363 2013-02-01 19:55
浏览 34
已采纳

模板中的Preg_Replace模式

I am struggling with a way to make this more efficient. Is it possible to make the loop into a single line?

Edit: looking to replace the loop.

$template = "<div>[@ data]</div>";
$arrayData = array('hello', 'hi', 'hola');

foreach ($arrayData as $dataValue) {
    $fillPattern = '/\\[@ data\\]/is';

    $arrayTemplate .= preg_replace($fillPattern, $dataValue, $template);
}

$viewContent = preg_replace($pattern, $arrayTemplate, $viewContent);
  • 写回答

2条回答 默认 最新

  • duanluo9369 2013-03-16 01:03
    关注

    Firstly, why you put the assignment to $fillPattern in the loop? Its value doesn't change for each iteration. You should put those statement outside the loop, before it. Like this:

    $template = "<div>[@ data]</div>";
    $arrayData = array('hello', 'hi', 'hola');
    $fillPattern = '/\\[@ data\\]/is'; //<-- put it here
    
    $arrayTemplate = '';
    foreach ($arrayData as $dataValue) {
        $arrayTemplate .= preg_replace($fillPattern, $dataValue, $template);
    }
    

    Secondly, you already have a way to make the loop into one line. Just use shorter variable names, remove the curly braces (because it is only one statement inside it), and write those 2 statements into one line. Like this:

    $tpl = "<div>[@ data]</div>";
    $data = array('hello', 'hi', 'hola');
    $pat = '/\\[@ data\\]/is';
    
    $res = ''; foreach ($data as $val) $res .= preg_replace($pat, $val, $tpl);
    

    But if you don't like the shorter variable names, good luck man. I don't think you can make a one line code with those long variable names. Except, you have a loooong looong line :D

    For the loop replacement, you should have an understanding about the functional programming. You can use array_map to replace the loop. To make it one line code (or one statement code I guess), you have to utilize anonymous function as the argument for array_map.

    With PHP 5.3 or newer you can use this code:

    $arrayTemplate = join('', array_map(
        function($x) use ($fillPattern, $template) {
            return preg_replace($fillPattern, $x, $template);
        }, $arrayData));
    

    But if you don't have PHP 5.3 or newer, you can use create_function like this:

    $arrayTemplate = join('', array_map(
        create_function('$x', 'global $fillPattern, $template;
            return preg_replace($fillPattern, $x, $template);'
        ), $arrayData));
    

    With the shorter variable names above, these code can be rewriten as follows:

    $res = join('', array_map(function($x) use ($pat, $tpl) {
        return preg_replace($pat, $x, $tpl);
    }, $data));
    

    or:

    $res = join('', array_map(create_function(
        '$x', 'global $pat, $tpl; return preg_replace($pat, $x, $tpl);'
    ), $data));
    

    You see, PHP was originally an imperative procedural language. It is not designed as a functional language in the first place. So, I guess the imperative way using foreach loop is still the better way to do it for now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄