Home > Backend Development > PHP Tutorial > PHP随机位置向字符串安插关键字

PHP随机位置向字符串安插关键字

WBOY
Release: 2016-06-13 13:08:02
Original
1080 people have browsed it

PHP随机位置向字符串插入关键字

$times = 4; //关键字出现频率
$keyword = "【fuck】"; //要替换的关键字
$str = "棕是呆硫酸铝中伙右中西医三阿拉山口地方就看到las决哦iasfls.sdmflaskdf洛杉矶的法律卡死噢iuoi020990拉开时间的发生了疯狂";
$strlen = mb_strlen($str, "gb2312");
//随机生成插入位置,根据$times决定插入频率
for ( $i = 0; $i {
$arr[] = mt_rand(0, $strlen);
}
$arr = array_unique($arr); //过滤数组重复元素
sort($arr); //对数组重新排序,按照值由小到大
//开始替换
$i = 0;
$str_new = "";
foreach( $arr as $v )
{
$str_new .= mb_substr($str, $i, $v - $i, "gb2312") . $keyword;
$i = $v;
}
$str_new .= mb_substr($str, $i, $strlen - $i, "gb2312");
echo($str_new);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template