您现在的位置: 365建站网 > 365文章 > PHP向一个字符串随机添加文字

PHP向一个字符串随机添加文字

文章来源:365jz.com 点击数:457 更新时间:2009-09-13 10:47

 很久前在baidu回答问题时写的“向一个字符串随机添加文字”的解决方案,可以支持中英文

<?php
/*********************************************************
 describe:字符串处理,可以处理中英文
 function:向一个字符串随机添加文字
 author:  etongchina
 email2me: etongchina@gmail.com
 time:    2008-06-24
 blog:    http://etongchina.blogcn.com
*********************************************************/

function getStrlen($str){
 $_count = array();
 $count = 0;
 for($count;$count<strlen($str);){
  ord($str{$i})>127?$count+=2:$count++;
  $_count[] = $count; //用数组存储可能插入的位置
 }
 $_key = array_rand($_count); //从数组中获取随机键
 return $_count[$_key];
}

$str = "伟大祖国繁荣富强"; //源字符串
$insert_str = "-";     //待添加的文字
$_rand = getStrlen($str);  //主函数
$_exp = "/^(.{".$_rand."})(.*)$/i"; //正则表达式
$t = preg_replace($_exp,"$1{$insert_str}$2",$str);
print $t;
?>

生产内容,同时打通搜索引擎(SEO)与AI搜索引擎(GEO)流量入口的软件! 点击进入365建站器 »

您可能感兴趣的文章