【问题标题】:php find replacephp查找替换
【发布时间】:2011-02-14 16:35:14
【问题描述】:

这更像是一个“最佳实践”问题。

我有一些电子邮件模板,其中有几个我需要查找和替换的标签。

例如:

Dear [[customername]],
Blah blah blah blah, and more blah.

Your Invoice....
----------------------------------------------------------
Order Status: [[orderstatus]]
Order Number: [[orderid]]
Date Ordered: [[dateordered]]
Payment Method: [[paymentmethod]]
Billing Statement: [[billingstatement]]

无论如何,我在双括号内有几个这样的标签。 所以我使用一个简单的: $text = str_replace($oldWord , $newWord , $text);

我确定这是执行此操作的正常方式,但我只是好奇是否有人有不同的想法。否则,我会坚持我正在做的事情。

【问题讨论】:

  • 在我看来这是解析模板最有效的方式。

标签: php str-replace


【解决方案1】:

您可以使用带有 str_replace 的数组:

$oldWords = array('[[customername]]',
                  '[[orderstatus]]'
                 );
$newWords = array($customerName,
                  $orderStatus
                 );

$newText = str_replace($oldWords , $newWords , $text); 

您也可以将 preg_replace 与数组一起使用

$oldWords = array('/\[\[customername\]\]/',
                  '/\[\[orderstatus\]\]/'
                 );
$newWords = array($customerName,
                  $orderStatus
                 );

$newText = preg_replace($oldWords , $newWords , $text); 

【讨论】:

    【解决方案2】:

    我无法从您的示例中确定,但如果 $oldWord 和 $newWord 是字符串(如其名称所示),那么您的做法不正确。 str_replace 接受数组参数,因此您可以一次完成所有替换:

    $text = str_replace($oldValues, $newValues, $text);
    

    其中 $oldValues = array('[[customername]]', [[orderstatus]], ...)

    and $newValues = array('John Smith', 'approved', ...)

    【讨论】:

      【解决方案3】:

      正如你所说,这没有对错,我通常会命名:

      _customername_
      

      没有特别的原因:)

      【讨论】:

      • 我认为他是从代码方面来看的,而不是在文本中区分变量的约定。
      【解决方案4】:

      您可以将数组与 str_replace 一起使用

      【讨论】:

        【解决方案5】:

        我会说,如果你的代码格式正确,就可以了,因为这是最快的方式。

        您也可以使用两个数组而不是两个字符串来传递给 str_replace,但在我看来,这不太可读。

        【讨论】:

          【解决方案6】:

          您可以使用词法分析器编写自己的扩展。 有关编写 php 扩展的教程,请访问http://devzone.zend.com/article/1021

          【讨论】:

          • 是的,因为他在 PHP 中挣扎,但在 C 中茁壮成长!
          【解决方案7】:

          我个人会像这样创建一个小模板类:

          class EmailTemplate
          {
              private $_template = '';
          
              public function __construct($template)
              {
                  $this->_template = $template;
              }
          
              public function __set($key,$value)
              {
                  $this->_template = str_replace("[[" . $key . "]]",$value,$this->_template);
              }
          
              public function __toString()
              {
                  return $this->_template;
              }
          }
          

          简单的用法是这样的:

          $temp = file_get_contents("email/templates/invoice.template");
          
          $Template = new EmailTemplate($temp);
          
          $Template->customername = "John Doe";
          $Template->orderstatus = "On Route";
          $Template->orderid = 14875356;
          $Template->dateordered = date();
          $Template->paymentmethod = "VISA (DD)";
          $Template->billingstatement = "blah";
          
          echo $Template; // = Happy.
          

          【讨论】:

            猜你喜欢
            • 2023-03-26
            • 1970-01-01
            • 2019-03-27
            • 2011-05-21
            • 2011-06-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-16
            相关资源
            最近更新 更多