Home >Backend Development >PHP Tutorial >php 遍历一个字符串,给每个字前加%

php 遍历一个字符串,给每个字前加%

WBOY
WBOYOriginal
2016-06-23 14:14:431433browse

例如:$str = "测试文字";

我要得到 %测%试%文%字%


回复讨论(解决方案)

$str = "测试文字";echo join('%', preg_split('//u', $str));
%测%试%文%字%

如果不是 utf-8 的,请自行完成编码转换

echo join("%",preg_split('/(?<!^)(?!$)/u','测试文字' ));

我测试 怎么不是“%测%试%文%字%”,还是“测试文字”呢

最好用个for 循环的

那你就是 gbk 编码的了
$s = '测试文字';
echo '%' . join('%', str_split($s, 2)) . '%'; //%测%试%文%字%

Statement:
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