文章任意位置随机加入特定字符串php代码
文章任意位置随机加入特定字符串php代码,使用教程: a href=http://www.tongqiong.com/read.php?tid-102.html rel=nofollowhttp://www.tongqiong.com/read.php?tid-102.html/a
文章任意位置随机加入特定字符串php代码,使用教程:
http://www.tongqiong.com/read.php?tid-102.html
<?php /*假设你的文字变量名为$str,需要插入的代码变量名为$ad[1], $ad[2], ..... 作者:http://www.tongqiong.com */ $ad[1]="同穷论坛"; $ad[2]="哪里购物"; $ad[3]="台州妈妈论坛"; $ad[4]="眼镜论坛"; $textlength = strlen($str); $curpos = ($textlength >= 100) ? rand(30, 100) : ($textlength > 30 ? rand(30, $textlength) :$textlength); $curadid = 1; while($curpos < $textlength){ $str = substr($str, 0, $curpos).$ad[$curadid].substr($str, $curpos, $textlength); $curpos += rand(20, 50) + strlen($ad[$curadid]); if($ad[$curadid + 1]) $curadid += 1; } /*这其中你有个没有说的地方,如果文本不够30个字符怎么办?不可能插入30 - 100之间吧。所以我加入个判断,不够30时加入末尾。 作者:http://www.tongqiong.com 另外,文章可能更长,比如10000个字符,却只有2段代码,我这个写法会自动插入代码2,如果已经没有代码3了。 */ ?>
精彩图集
精彩文章