【问题标题】:Spinner problem - PHP foreach微调器问题 - PHP foreach
【发布时间】:2011-07-20 17:54:01
【问题描述】:

我在自定义找到的脚本时遇到问题。

脚本如下所示:

function spin($var){
$words = explode("{",$var);
foreach ($words as $word)
{
    $words = explode("}",$word);
    foreach ($words as $word)
    {
        $words = explode("|",$word);
        $word = $words[array_rand($words, 1)];        
        echo $word." ";

    }

}
}

$text = "Digitalpoint.com is {the best forum|a great Forum|a wonderful Forum|a perfect Forum} {123|some other sting}";
spin($text);

我想自定义脚本以返回结果的值。

例子:

$spin = spin($text);
echo $spin;

我尝试通过

生成结果变量
$output = $output + $word." ";
return $output;

然后

$spin = spin($text);
echo $spin;

但我总是得到结果“0”...谁能想出一个聪明的解决方案来解决这个问题?我期待任何提示/提示,在此先感谢!

【问题讨论】:

    标签: php foreach return


    【解决方案1】:

    试试这个。 spun 函数没有返回值。我们不使用echo,而是将结果附加到字符串$spun 并返回。

    function spin($var){
    $spun = "";
    
    $words = explode("{",$var);
    foreach ($words as $word)
    {
        $words = explode("}",$word);
        foreach ($words as $word)
        {
            $words = explode("|",$word);
            $word = $words[array_rand($words, 1)];        
            $spun .= $word." ";
        }
    }
    
    return $spun;
    }
    

    【讨论】:

      【解决方案2】:

      这个位表示 $output 是 $output 和 $word 的总和:

      $output = $output + $word." ";
      return $output;
      

      因为它们不是数字,所以返回 0。

      尝试使用这些语句:

      $output .= $word . " ";
      return $output;
      

      【讨论】:

        【解决方案3】:

        问题不在于您提供的代码,而在于您稍后指定的 return 语句。

        $output .= $word." ";
        return $output;
        

        【讨论】:

          【解决方案4】:

          我更改了函数,这样您就不会重复变量名

          function spin($var){
            $words = explode("{",$var);
            foreach ($words as $word)
            {
              $words2 = explode("}",$word);
              foreach ($words2 as $word2)
              {
                $words3 = explode("|",$word2);
                $word3 = $words3[array_rand($words3, 1)];
                echo $word3." ";
          
              }
            }
          }
          

          我似乎从文本中得到随机短语。这是你想要的吗?

          【讨论】:

            【解决方案5】:

            这也可以通过 preg_replace_callback 来完成:

            function spin($text)
            {
                return preg_replace_callback('/\{(.+?)\}/',
                                             function($matches) {
                                                $values = explode('|', $matches[1]);
                                                return $values[array_rand($values)];
                                             },
                                             $text);
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-06-27
              • 1970-01-01
              • 2017-07-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-08-04
              • 2017-05-18
              相关资源
              最近更新 更多