Php 使用str_replace替换子字符串

Php 使用str_replace替换子字符串,php,str-replace,Php,Str Replace,我只是想让我的头绕过stru_替换和卷曲的括号/支架。 通过下面的一行,我知道键入{the_title}将替换为数组$some_runtime_generated_title,但是第一个值($str_template)是什么意思呢 说我想做以下 $dog='lassy'; {dog} would output >> lassy 在php中我将如何做到这一点?您实际上自己给出了答案: <?php $some_runtime_generated_title = 'generate

我只是想让我的头绕过stru_替换和卷曲的括号/支架。 通过下面的一行,我知道键入{the_title}将替换为数组$some_runtime_generated_title,但是第一个值($str_template)是什么意思呢

说我想做以下

$dog='lassy';
{dog}
would output >> lassy

在php中我将如何做到这一点?

您实际上自己给出了答案:

<?php
$some_runtime_generated_title = 'generated title';
$str_template = 'This is the template in which you use {the_title}';
$parsed = str_replace( '{the_title}', $some_runtime_generated_title, $str_template );
echo $parsed;

你自己几乎给出了答案:

<?php
$some_runtime_generated_title = 'generated title';
$str_template = 'This is the template in which you use {the_title}';
$parsed = str_replace( '{the_title}', $some_runtime_generated_title, $str_template );
echo $parsed;

一个简单的str_replace替换占位符的用例如下所示:

$paramNames = array("{name}", "{year}", "{salutation}");
$paramValues = array("Iain Simpson", "2012", "Alloha");
$text = "{salutation}, {name}! Happy {year}!";
str_replace($paramNames, $paramValues, $text);
$paramNames
$paramValues
数组具有相同数量的值

一个更具体的功能是:

/* Processes a text template by replacing {param}'s with corresponding values. A variation fo this function could accept a name of a file containing the template text. */
/* Parameters: */
/*   template - a template text */
/*   params   - an assoc array (or map) of template parameter name=>value pairs */
function process_template($template, $params) {
    $result = $template;
    foreach ($params as $name => $value) {
        // echo "Replacing {$name} with '$value'"; // echo can be used for debugging purposes
        $result = str_replace("{$name}", $value, $result);
    }
    return $result;
}
用法示例:

$text = process_template("{salutation}, {name}! Happy {year}!", array(
    "name" => "Iain", "year" => 2012, "salutation" => "Alloha"
));
$template = new TextTemplate("{salutation}, {name}! Happy {year}!");
$text = $template->apply(array("name" => "Iain", "year" => 2012, "salutation" => "Alloha"));
下面是一个面向对象方法的示例:

class TextTemplate {
   private static $left = "{";
   private static $right = "}";
   private $template;

   function __construct($template) {
       $this->template = $template;
   }

   public function apply($params) {
       $placeholders = array();
       $values = array();
       foreach($params as $name => $value) {
           array_push($placeholders, self::$left . $name . self::$right);
           array_push($values, $value);
       }
       $result = str_replace($placeholders, $values, $this->template);
       return $result;
   }
}
用法示例:

$text = process_template("{salutation}, {name}! Happy {year}!", array(
    "name" => "Iain", "year" => 2012, "salutation" => "Alloha"
));
$template = new TextTemplate("{salutation}, {name}! Happy {year}!");
$text = $template->apply(array("name" => "Iain", "year" => 2012, "salutation" => "Alloha"));

一个简单的str_replace用于place holder replace的用例如下所示:

$paramNames = array("{name}", "{year}", "{salutation}");
$paramValues = array("Iain Simpson", "2012", "Alloha");
$text = "{salutation}, {name}! Happy {year}!";
str_replace($paramNames, $paramValues, $text);
$paramNames
$paramValues
数组具有相同数量的值

一个更具体的功能是:

/* Processes a text template by replacing {param}'s with corresponding values. A variation fo this function could accept a name of a file containing the template text. */
/* Parameters: */
/*   template - a template text */
/*   params   - an assoc array (or map) of template parameter name=>value pairs */
function process_template($template, $params) {
    $result = $template;
    foreach ($params as $name => $value) {
        // echo "Replacing {$name} with '$value'"; // echo can be used for debugging purposes
        $result = str_replace("{$name}", $value, $result);
    }
    return $result;
}
用法示例:

$text = process_template("{salutation}, {name}! Happy {year}!", array(
    "name" => "Iain", "year" => 2012, "salutation" => "Alloha"
));
$template = new TextTemplate("{salutation}, {name}! Happy {year}!");
$text = $template->apply(array("name" => "Iain", "year" => 2012, "salutation" => "Alloha"));
下面是一个面向对象方法的示例:

class TextTemplate {
   private static $left = "{";
   private static $right = "}";
   private $template;

   function __construct($template) {
       $this->template = $template;
   }

   public function apply($params) {
       $placeholders = array();
       $values = array();
       foreach($params as $name => $value) {
           array_push($placeholders, self::$left . $name . self::$right);
           array_push($values, $value);
       }
       $result = str_replace($placeholders, $values, $this->template);
       return $result;
   }
}
用法示例:

$text = process_template("{salutation}, {name}! Happy {year}!", array(
    "name" => "Iain", "year" => 2012, "salutation" => "Alloha"
));
$template = new TextTemplate("{salutation}, {name}! Happy {year}!");
$text = $template->apply(array("name" => "Iain", "year" => 2012, "salutation" => "Alloha"));


您是否正在寻找一个PHP模板引擎(本着MustacheJS的精神)?这种问题很容易回答。它甚至有例子。我不明白人们是如何在没有手册的情况下编写代码的。在我来到这里之前,我阅读了手册,但我不明白,阅读一些东西和理解它们是完全不同的。你是在寻找一个PHP模板引擎(本着MustacheJS的精神)吗?这种问题很容易回答。它甚至有例子。我不明白人们是如何在没有手册的情况下编写代码的。我来这里之前读过手册,但我不明白,读一些东西和理解它们是完全不同的。我想,
$stru template
应该是最后一个参数。对不起,我从另一篇文章中复制了代码,我不是自己写的,这只是一个例子。这是否意味着$str_模板只是一个数组?。第一部分是一个值数组,第二部分是要替换的“关键字”,第三位是要替换的值,这与第一个数组有什么关系?第一个数组的最后一部分是什么?比如:$names{nameis}$nameis='frank'会回显frank?您应该将str_replace读作:str_replace($keywords,$with_values,$in_string)@IainSimpson@Vlad My bad,我把参数搞混了。它应该是
str_replace($search,$replace,$subject)
。我在示例中修复了它。@Iain
$str_模板
就是“整体”包含必须替换的单词的文本。我想,
$str_template
应该是最后一个参数。对不起,我从另一篇文章中复制了该代码,我不是自己写的,它只是一个示例。这是否意味着$str_template只是一个数组?因此第一部分是一个值数组,第二部分是您想要的“关键字”要替换的t,第三位是要替换的值,它与第一个数组的关系如何?是第一个数组的最后一个数组部分。例如:$names{nameis}$nameis='frank'将响应frank?您应该将str_replace读为:str_replace($keywords,$with_values,$in_string)@IainSimpson@Vlad My bad,我把参数搞混了。它应该是
str\u replace($search,$replace,$subject)
。我在示例中修复了它。@Iain
$str\u template
就是“整体”包含必须替换的单词的文本。啊,这更有意义,谢谢你的帮助,我将在一段时间内对它进行一次尝试,看看我能做些什么,它看起来对电子邮件模板和其他东西非常有用,但我昨天才第一次读到它,大多数解释都很混乱。我如何获得替换要回显?,我尝试了回显“{name}”;但那只会给我{name},我也尝试了{name},但这给了我一个php错误。@Iain,我不确定您想要实现什么,但是一个简单的变量回显可以作为
echo“{$name}”来完成
.Ahh这更有意义,谢谢你的帮助,我会马上试用一下,看看我能做些什么,它看起来对电子邮件模板和其他东西很有用,但我昨天才第一次读到它,大多数解释都很混乱。我如何让替换者回显?我已经试过回显“{name}”“;但这只给了我{name},我也尝试了{name},但这给了我一个php错误。@Iain,我不确定你想要实现什么,但是一个简单的变量回显可以通过
回显“{$name}”
来完成。