如何解决如何确定计数并根据结果编辑随机数?
| 这是我当前的php脚本。$mystr = \'Hello world. The world is nice\';
substr_count($mystr,\'world\'); // count=2;
$mytext = preg_replace(\'/\\b(\'.$mystr.\')\\b/i\',\'<b>$1</b>\',$mytext);
我想随机地将其中一个用黑体字显示。
有什么聪明的方法吗?
在高级,史蒂文Thx
解决方法
您是否要说一个特定的单词,即
world
粗体字?如果是,一种方法可能是:
function make_bold($str,$replace) {
$words = array_intersect(str_word_count(strtolower($str),2),$replace);
if(count($words) > 0) {
$rand_pos = array_rand($words);
$str = substr_replace($str,\'<b>\' . $words[$rand_pos] . \'</b>\',$rand_pos,strlen($words[$rand_pos]));
}
return $str;
}
用作:
$str = make_bold($str,array(\'world\'));
参考:str_word_count
,array_intersect
,array_rand
,substr_replace
观看演示。
, $mystr = \'Hello world. The world is nice\';
$words = explode(\' \',$mystr);
$randWordKey = rand(0,count($words));
$words[$randWordKey] = \'<b>\' . $words[$randWordKey] . \'</b>\';
$mytext = implode(\' \',$words);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。