更新对不起。我不像我想的那么清楚。我基本上是想插入更多的文本字符串。该函数在wordpress中,并从广告仪表板中提取html代码。
<ul id="randomOrderList">
<li><?php if (function_exists('dfrads')) { echo dfrads('1'); } ?></li>
<li><?php if (function_exists('dfrads')) { echo dfrads('2'); } ?></li>
<li><?php if (function_exists('dfrads')) { echo dfrads('3'); } ?></li>
</ul>对此:
<ul id="randomOrderList">
<li><a href="" title=""><img src="name" alt=" " /></a></li>
<li><a href="" title=""><img src="name" alt=" " /></a></li>
<li><a href="" title=""><img src="name" alt=" " /></a></li>
</ul>我尝试过这样做,但不知道如何在数组中添加php:
<?
$array=array('<?php if (function_exists('dfrads')) { echo dfrads('1'); } ?>','<?php if (function_exists('dfrads')) { echo dfrads('2'); } ?>','<?php if (function_exists('dfrads')) { echo dfrads('3'); } ?>');
shuffle($array);
$array = array_chunk($array, 5);
foreach($array as $section){
echo '<ul id="randomOrderList">'."\n";
foreach($section as $link){
echo ' <li>'.$link.'</li>'."\n";
}
echo '</ul>'."\n";
}
?>发布于 2011-03-26 13:57:15
您可以将字符串放入数组中:
$strings = array(
'something 1',
'something 2',
'something 3',
);没有任何类型的代码-- 在那里是不允许的.
然后,使用函数将数组随机化:
shuffle($strings);现在,在数组上循环,以显示项:
echo '<ul id="randomOrderList">';
foreach ($strings as $string) {
echo '<li>' . $string . '</li>';
}
echo '</ul>';它们将按随机顺序显示。
当然,在编辑完问题:编辑之后,您可以构造调用函数的数组。
例如,您可以使用以下内容:
$strings = array(
dfrads('1'),
dfrads('2'),
dfrads('3'),
);如果需要,可以将所有代码包含在测试中,以确定是否存在dfrads函数:
if (function_exists('dfrads')) {
// Create the array
// randomize it with shuffle
// do the loop and the echos
}发布于 2011-03-26 13:55:48
$list = array('something 1', 'something 2', 'something 3');
shuffle($list);
echo '<ul>';
foreach ($list as $item) {
echo '<li>' . htmlspecialchars($item) . '</li>';
}
echo '</ul>';https://stackoverflow.com/questions/5442750
复制相似问题