微调器问题-PHP foreach [英] Spinner problem - PHP foreach
本文介绍了微调器问题-PHP foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在对找到的脚本进行编排时遇到问题.
I have a problem costomizing a script i found.
该脚本如下所示:
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);
我想自定义脚本以返回结果的值.
I'd like to customize the script to return the value of the result.
示例:
$spin = spin($text);
echo $spin;
我试图通过生成一个结果变量
I've tried to generate a result variable by
$output = $output + $word." ";
return $output;
然后
$spin = spin($text);
echo $spin;
但是我总是得到结果"0" ...有人可以为这个问题提出一个聪明的解决方案吗?我期待任何提示/提示,谢谢!!
But i've always got result "0"... Can anyone come up with a clever solution for this problem? I'm looking forward to any tips/hints, thanks in advance!
推荐答案
尝试一下. spun
函数未返回值.代替使用echo
,我们将结果附加到字符串$spun
上并返回.
Try this. The spun
function wasn't returning a value. Instead of using echo
, we'll just append the results onto string $spun
and return that.
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;
}
这篇关于微调器问题-PHP foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文