您的位置:首页 > 编程语言 > PHP开发

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

2009-09-12 23:00 197 查看
很久前在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;

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: