Home > Backend Development > PHP Tutorial > 修改一段代码,高手指教了

修改一段代码,高手指教了

WBOY
Release: 2016-06-23 14:38:17
Original
886 people have browsed it

$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

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