【问题标题】:How to dynamically create a string in PHP?如何在 PHP 中动态创建字符串?
【发布时间】:2012-11-06 05:30:44
【问题描述】:

我有一个用于构建字符串的预定义模式,应该在整个脚本中定期创建。

$str="$first - $second @@ $third"; // pattern for building the string
$first="word1";
$second="word2";
$third="word3";
$string= ..?? // string should be built here based on the pattern

目前,我正在使用eval 根据最初定义的模式就地生成字符串。但是,由于这种情况偶尔会发生,并且eval 通常不好,我希望找到另一种方法。

请注意,模式在所有代码之上仅定义一次,我可以仅通过一行编辑所有脚本的模式。因此,任何更改都不应触及 $string 的原因

我尝试了create_function,但需要相同数量的参数。使用eval,我可以轻松更改模式,但使用create-function,我需要更改整个脚本。例如,如果将字符串模式更改为

$str="$first @@ $second"; // One arg/var is dropped

评估示例:

$str="$first - $second @@ $third"; // Pattern is defined one-time before codes
$first="word1";
$second="word2";
$third="word3";
eval("\$string = \"$str\";");

create_function 示例:

$str=create_function('$first,$second,$third', 'return "$first - $second @@ $third";');
$string=$str($first,$second,$third);

【问题讨论】:

  • 你能举出eval的例子吗?有点难以理解您到底要做什么,因为最后一个字符串声明看起来应该可以满足您的需求。
  • @Vulcan 我添加了示例来澄清问题。
  • 谢谢,他们帮助澄清了你的问题,我已经回答了这个问题。

标签: php string function variables eval


【解决方案1】:

您可以使用sprintfvsprintf 提供的字符串格式化功能。

$format = "%s - %s @@ %s"; // pattern for building the string
$first = "word1";
$second = "word2";
$third = "word3";
$string = sprintf($format, $first, $second, $third);

如果你想传递一个数组,你可以使用vsprintf

$format = "%s - %s @@ %s"; // pattern for building the string
$values = array($first, $second, $third);
$string = vsprintf($format, $values);

【讨论】:

  • 微妙的解决方案,但是如果改变模式中变量的顺序呢?例如:$str="$second-$first";然后,我们还需要在整个脚本中更改sprintf 模式。
  • 您可以在格式中指定要引用的参数编号:$str = "%2$s-%1$s"。这会将第二个(包括 $format 本身的第三个)参数放在输出顺序中的第一个参数之前。
  • 现在,这是一个实用的解决方案。我很欣赏你提到sprintf的微妙观点,这是模式化字符串的关键功能,但我一味地忽略了。
【解决方案2】:

对我来说似乎是相当简单的事情。使用str_replace()并根据模式替换

$str="$first$ - $second$ @@ $third$"; // pattern for building the string
$first="word1";
$second="word2";
$third="word3";

$newstr = str_replace('$first$', $first, $str);
$newstr = str_replace('$second$', $second, $newstr);
$newstr = str_replace('$third$', $third, $newstr);

【讨论】:

  • 1.模式并不总是简单的,2.我只想对模式进行更改,但是按照你的方法,我需要在整个脚本中修改$newstr,而且很多次。
【解决方案3】:

这可能有帮助,不确定。

$str = "_first_ - _second_ @@ _third_"; // pattern for building the string
// _first, _second_... are placeholders for actual values

// this function performs the string building
function buildString( $arr ) {
   global $str;

   $keys = array_keys( $arr );
   $vals = array_values( $arr );

   return eval( str_replace( $keys, $vals, $str ) );
}

while(some condition here) {
    $str = 'A new string'; // you can change your string builiding pattern here

    // this array is built in the fashion ->
    // keys of array are the variables in $str and
    // their actual values are the values of $str
    $arr=array('_first_' => $first, '_second_' => $second, '_third_' => $third);
    echo buildString( $arr );
}

【讨论】:

  • 目前,eval 运行良好。关键是eval 不是一件好事。我想在没有eval 的情况下执行此操作,但您的功能也基于eval
猜你喜欢
  • 2022-06-25
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2011-01-13
  • 2013-08-15
  • 1970-01-01
相关资源
最近更新 更多