[英]How to echo randomly a portion of a string?
i have already succesfully translated some quotes via my translation function __();
我已经通过翻译函数__();
成功翻译了一些引号__();
and now I want to echo only one of those quotes at random . 现在我只想随意回声其中的一句话 。 All quotes are separated in this string with a special character like a |
该字符串中的所有引号均以特殊字符(例如|
分隔。
Sofar I only have this. Sofar我只有这个。 What code could should go below this tackle my random echo? 在解决我的随机回声之后,应该使用什么代码?
$quotes =
__("IF YOU MAKE EVERYTHING BOLD, NOTHING IS BOLD") . "|" .
__("Quality of design is an indicator of credibility") . "|" .
__("People ignore design, that ignores people");
(An important restriction: it is essential that the quotes be exactly closed with __("
and ");
sothat they can be checked and translated.) __($variable)
doest not work with current clean up scripts that I have bought so these won't work. (一个重要的限制:必须使用__("
和");
精确地将引号引起来");
以便可以对其进行检查和翻译。) __($variable)
不适用于我购买的当前清理脚本,因此这些将无法正常工作。
You're already calling __()
on each of your quotes individually, why not save all the extra translating and do something like: 您已经在每个引号上分别调用__()
,为什么不保存所有额外的翻译并执行类似的操作:
$quotes = array('quote1', 'quote2', 'quote3');
$index = array_rand($quotes);
echo __($quotes[$index]);
Edit: To satisfy your other requirement, that the call to __()
must immediately surround each string, you could do this: 编辑:为了满足您的其他要求,对__()
的调用必须立即包围每个字符串,您可以这样做:
$quotes = array(__('quote1'), __('quote2'), __('quote3'));
$index = array_rand($quotes);
echo $quotes[$index];
The big downside here is that you're now looking up a translation for every string in that array, even though only one is printed, but that's the same situation you had in the "one big string" solution. 此处最大的缺点是,尽管只打印了一个字符串,但您现在正在为该数组中的每个字符串查找翻译,但这与您在“一个大字符串”解决方案中的情况相同。
Why the biscuits are they all in one string, and not an array? 为什么饼干全都在一个字符串中,而不是一个数组中? Your problem would be immediately solved if this was the case. 如果是这种情况,您的问题将立即得到解决。 As stands, split in |
站在看台上|
and index randomly into the array created to pick a random quote. 然后将其随机索引到创建的数组中以选择随机报价。
Why don't you keep them in an array and translate only what is actually outputted? 为什么不将它们保留在数组中,只翻译实际输出的内容?
$quotes = array(
"IF YOU MAKE EVERYTHING BOLD, NOTHING IS BOLD",
"Quality of design is an indicator of credibility",
"People ignore design, that ignores people",
);
$randomQuote = $quotes[ rand(0, count($quotes)-1)];
echo __($randomQuote);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.