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

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

WBOY
Release: 2016-06-23 14:14:43
Original
1436 people have browsed it

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

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


回复讨论(解决方案)

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

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

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

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

最好用个for 循环的

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

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