您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP随机插入关键字到有HTML的内容该如何实现?

2024/3/29 6:04:21发布20次查看
因为是随机位置,也有可能插入到 
这种地方,这个问题该怎么解决?
能否先剥离html标签,随机插入后,再还原呢?希望大家帮帮我。
回复讨论(解决方案) utf-8下测试没问题
$str = <$v){ $insertk=insertk($len-1,$delay); $str_arr[$insertk].=$insert[$k]; } echo join('',$str_arr);}function insertk($count,$delay){ $insertk=rand(0,$count); if(in_array($insertk,$delay)){ $insertk=insertk($count,$delay); } return $insertk;}
要有注释就好了
好吧,注释
function randominsert($txt,$insert){//txt 内容;insert要插入的关键字,可以是链接,数组 //将内容拆分成数组,每个字符都是一个value,英文,中文,符号都算一个,只能在utf-8下中文才能拆分 preg_match_all(/[\x01-\x7f]|[\xe0-\xef][\x80-\xbf]{2}/, $txt, $match); $delay=array(); $add=0; //获取不能插入的位置索引号($delay 数组),也就是 之间的位置 foreach($match[0] as $k=>$v){ if($v=='') $add=0; } $str_arr=$match[0]; $len=count($str_arr); foreach($insert as $k=>$v){ //获取随机插入的位置索引值 $insertk=insertk($len-1,$delay); //循环将insert数据 拼接到 随机生成的索引 $str_arr[$insertk].=$insert[$k]; } //合并插入 关键词后的数据,拼接成一段内容 echo join('',$str_arr);}function insertk($count,$delay){//count 随机索引值范围,也就是内容拆分成数组后的总长度-1;delay 不允许的随机索引值,也就是不能在 之间 $insertk=rand(0,$count); if(in_array($insertk,$delay)){//索引值不能在 不允许的位置处(也就是 之内的索引值) $insertk=insertk($count,$delay);//递归调用,直到随机插入的索引值不在 这个索引值数组中 } return $insertk;}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product