$char = "<div>11111</div><div>22222</div><div>33333</div>";preg_match_all("|<div>(.*)</div>|U", $char, $out,PREG_PATTERN_ORDER);echo $out[0][0];echo $out[0][1];echo $out[0][2];
Copy after login
上面这段代码可以获取字符串之间的值,但我要的效果是
我不知道字符串里有多少个
nnnnn
,我想获取所有div之间的所有值并且随机打乱顺序
比如字符串"
11111
22222
33333
44444
";
我想得到的结果是:
22222
44444
33333
11111
麻烦知道的高手直接给完整代码,谢谢了
回复讨论(解决方案)
补充:div之间有中文字符
$char = "<div>11111</div><div>22222</div><div>33333</div>";$ary = explode('<div>', $char);shuffle($ary);foreach($ary as $v){ echo '<div>'.$v;}
Copy after login
$char = "
11111
22222
33333
";
$ary = explode('
', $char);
shuffle($ary);
foreach($ary as $v)
{
if($v!=""){
echo '
'.$v;
}
}
楼上的加个判断值不为空更好点,数组后面会多循环一次,后面会多带个
谢谢楼上的两位大哥,这是一个循环
那怎么把这个值重新放到字符串里
比如$char1 = ''
代码应当怎么写,小弟不怎么懂php的,再麻烦下,谢谢了
$char = "<div>11111</div><div>22222</div><div>33333</div>";preg_match_all("|<div>(.*)</div>|U", $char, $out,PREG_PATTERN_ORDER);shuffle($out[0]);$s= join('',$out[0]);echo $s;
Copy after login
靠
用5楼的代码
$char = "<div>11111</div><div>22222</div><div>33333</div>";preg_match_all("|<div>(.*)</div>|U", $char, $out,PREG_PATTERN_ORDER);shuffle($out[0]);$char= join('',$out[0]);echo $char;
Copy after login
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31