2013-02-07 30 views
2

我有一个观点论坛后脚本插入字符串的foreach结果

if(isset($replys)){ 
    foreach ($replys as $reply) { 
     echo $reply['name'].' | '.$reply['date']; 
    } 
} 

输出,如:

abc | 2013-1-1 
123 | 2013-1-2 
456 | 2013-1-3 

那么我想随机显示的广告 例如:

abc | 2013-1-1 
Google Ads | testing 
123 | 2013-1-2 
456 | 2013-1-3 

使用一些if语句:

if(is_string(substr(sha1(uniqid()),-1))){ 
// show ads 
} 

我该怎么做?

+0

如果(IS_STRING(SUBSTR(SHA1(uniqid()), - 1))){// 显示广告 }将这个生成随机字符串 –

回答

1

找到一个随机点插入广告:

if (is_string(substr(sha1(uniqid()),-1))) { 
    // show ads 
    $offset = mt_rand(0, count($replys) - 1); 
    $ad = array('name' => 'Google Ads', 'date' => 'testing'); 

    $replys = array_splice($replys, $offset, 0, $ad); 
} 
+0

我可以在回复中插入广告到随机位置吗? –

+0

@jkjk答案更新。 –