如何用 PHP 替换字符串中的多个 %tags%

发布于 2024-09-07 22:54:58 字数 378 浏览 4 评论 0原文

替换 PHP 字符串中一组短标签的最佳方法是什么,例如:

$return = "Hello %name%, thank you for your interest in the %product_name%.  %representative_name% will contact you shortly!";

我将定义 %name% 是某个字符串,来自数组或对象,例如:

$object->name;
$object->product_name;

等等。

我知道我可以运行 str_replace在字符串上多次,但我想知道是否有更好的方法来做到这一点。

谢谢。

What is the best way of replacing a set of short tags in a PHP string, example:

$return = "Hello %name%, thank you for your interest in the %product_name%.  %representative_name% will contact you shortly!";

Where I would define that %name% is a certain string, from an array or an object such as:

$object->name;
$object->product_name;

etc..

I know I could run str_replace multiple times on a string, but I was wondering if there is a better way of doing that.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偷得浮生 2024-09-14 22:54:58

如果您知道要替换的占位符,str_replace() 似乎是一个理想的选择。这需要运行一次而不是多次。

$input = "Hello %name%, thank you for your interest in the %product_name%.  %representative_name% will contact you shortly!";

$output = str_replace(
    array('%name%', '%product_name%', '%representative_name%'),
    array($name, $productName, $representativeName),
    $input
);

str_replace() seems an ideal option if you know the placeholders you intend to replace. This need be run just once not multiple times.

$input = "Hello %name%, thank you for your interest in the %product_name%.  %representative_name% will contact you shortly!";

$output = str_replace(
    array('%name%', '%product_name%', '%representative_name%'),
    array($name, $productName, $representativeName),
    $input
);
梦晓ヶ微光ヅ倾城 2024-09-14 22:54:58

这个类应该这样做:

<?php
class MyReplacer{
  function __construct($arr=array()){
    $this->arr=$arr;
  }

  private function replaceCallback($m){
    return isset($this->arr[$m[1]])?$this->arr[$m[1]]:'';
  }

  function get($s){  
    return preg_replace_callback('/%(.*?)%/',array(&$this,'replaceCallback'),$s);
  }

}


$rep= new MyReplacer(array(
    "name"=>"john",
    "age"=>"25"
  ));
$rep->arr['more']='!!!!!';  
echo $rep->get('Hello, %name%(%age%) %notset% %more%');

This class should do it:

<?php
class MyReplacer{
  function __construct($arr=array()){
    $this->arr=$arr;
  }

  private function replaceCallback($m){
    return isset($this->arr[$m[1]])?$this->arr[$m[1]]:'';
  }

  function get($s){  
    return preg_replace_callback('/%(.*?)%/',array(&$this,'replaceCallback'),$s);
  }

}


$rep= new MyReplacer(array(
    "name"=>"john",
    "age"=>"25"
  ));
$rep->arr['more']='!!!!!';  
echo $rep->get('Hello, %name%(%age%) %notset% %more%');
廻憶裏菂餘溫 2024-09-14 22:54:58

最简单和最短的选项是带有“e”开关的 preg_replace

$obj = (object) array(
    'foo' => 'FOO',
    'bar' => 'BAR',
    'baz' => 'BAZ',
);

$str = "Hello %foo% and %bar% and %baz%";
echo preg_replace('~%(\w+)%~e', '$obj->$1', $str);

The simplest and shortest option is preg_replace with the 'e' switch

$obj = (object) array(
    'foo' => 'FOO',
    'bar' => 'BAR',
    'baz' => 'BAZ',
);

$str = "Hello %foo% and %bar% and %baz%";
echo preg_replace('~%(\w+)%~e', '$obj->$1', $str);
圈圈圆圆圈圈 2024-09-14 22:54:58

来自 str_replace 的 PHP 手册:

如果searchreplace是数组,那么
str_replace() 从每个中获取一个值
数组并使用它们进行搜索和
替换主题。如果替换有
比搜索更少的值,然后
其余部分使用空字符串
替换值。如果搜索是一个
array 和replace 是一个字符串,那么
该替换字符串用于
搜索的每个值。反过来
但这没有意义。

http://php.net/manual/en/function.str-replace。 php

From the PHP manual for str_replace:

If search and replace are arrays, then
str_replace() takes a value from each
array and uses them to do search and
replace on subject. If replace has
fewer values than search, then an
empty string is used for the rest of
replacement values. If search is an
array and replace is a string, then
this replacement string is used for
every value of search. The converse
would not make sense, though.

http://php.net/manual/en/function.str-replace.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文