Home > Backend Development > PHP Tutorial > 返回字符串截取循环

返回字符串截取循环

WBOY
Release: 2016-06-23 14:21:27
Original
794 people have browsed it

PHP 字符串 return 

$return 返回值为
 9||50
||1|#|2013-09-09 00:00:22||13814002394||内容
||1|#|2013-09-09 00:00:19||13814002394||内容
||1|#|2013-09-07 00:51:57||15920531400||内容
||1|#|2013-09-06 22:21:02||15920531400||内容
||1|#|2013-09-06 22:13:14||15920531400||内容
||1|#|2013-09-06 22:07:42||15920531400||内容
||1|#|2013-09-05 14:00:02||15920531400||内容
||1|#|2013-09-05 12:10:13||15920531400||内容
||1|#|2013-09-05 12:01:27||15920531400||内容
||1|#|

//这是整理过的

是有规则的字符串(不是数组)
我变成可循环的数组 其中时间,号码,内容是变量!!!!!


回复讨论(解决方案)

到前台我想要的结果是

  • 2013-09-09 00:00:22    13814002394   内容

  • 2013-09-09 00:00:22    13814002394   内容

  • 2013-09-09 00:00:22    13814002394   内容

  • 这样的循环方式!

    <?php	$str='9||50	||1|#|2013-09-09 00:00:22||13814002394||内容	||1|#|2013-09-09 00:00:19||13814002394||内容	||1|#|2013-09-07 00:51:57||15920531400||内容	||1|#|2013-09-06 22:21:02||15920531400||内容	||1|#|2013-09-06 22:13:14||15920531400||内容	||1|#|2013-09-06 22:07:42||15920531400||内容	||1|#|2013-09-05 14:00:02||15920531400||内容	||1|#|2013-09-05 12:10:13||15920531400||内容	||1|#|2013-09-05 12:01:27||15920531400||内容	||1|#|';	$new_str=str_replace('||','|',$str);	$arr=explode('|1|#|',$new_str);	$new_arr=array();	foreach ($arr as $value) 	{		echo $value;		if (substr_count($value,'|')>1) 		{			$new_arr[]=explode('|',$value);		}	}	echo "<pre class="brush:php;toolbar:false">";	print_R($new_arr);	echo "
    Copy after login
    ";?>
    简单的写了下,凑合着能,不知道有没有什么更好的办法
    Copy after login

    Array(    [0] => Array        (            [0] => 2013-09-09 00:00:22            [1] => 13814002394            [2] => 内容        )    [1] => Array        (            [0] => 2013-09-09 00:00:19            [1] => 13814002394            [2] => 内容        )    [2] => Array        (            [0] => 2013-09-07 00:51:57            [1] => 15920531400            [2] => 内容        )    [3] => Array        (            [0] => 2013-09-06 22:21:02            [1] => 15920531400            [2] => 内容
    Copy after login


    )这是打印出来的效果

    $s =<<< TXT9||50||1|#|2013-09-09 00:00:22||13814002394||内容||1|#|2013-09-09 00:00:19||13814002394||内容||1|#|2013-09-07 00:51:57||15920531400||内容||1|#|2013-09-06 22:21:02||15920531400||内容||1|#|2013-09-06 22:13:14||15920531400||内容||1|#|2013-09-06 22:07:42||15920531400||内容||1|#|2013-09-05 14:00:02||15920531400||内容||1|#|2013-09-05 12:10:13||15920531400||内容||1|#|2013-09-05 12:01:27||15920531400||内容||1|#|TXT;preg_match_all('/\|([^|]+)\|\|([^|]+)\|\|(.*)/m', $s, $r);print_r($r);
    Copy after login
    Array(    [1] => Array        (            [0] => 2013-09-09 00:00:22            [1] => 2013-09-09 00:00:19            [2] => 2013-09-07 00:51:57            [3] => 2013-09-06 22:21:02            [4] => 2013-09-06 22:13:14            [5] => 2013-09-06 22:07:42            [6] => 2013-09-05 14:00:02            [7] => 2013-09-05 12:10:13            [8] => 2013-09-05 12:01:27        )    [2] => Array        (            [0] => 13814002394            [1] => 13814002394            [2] => 15920531400            [3] => 15920531400            [4] => 15920531400            [5] => 15920531400            [6] => 15920531400            [7] => 15920531400            [8] => 15920531400        )    [3] => Array        (            [0] => 内容            [1] => 内容            [2] => 内容            [3] => 内容            [4] => 内容            [5] => 内容            [6] => 内容            [7] => 内容            [8] => 内容        ))
    Copy after login

    已经可以用了
    如果想把对应的放在一起就
    $ar = array_map(function($a,$b,$c){return array($a,$b,$c);}, $r[1], $r[2], $r[3]);print_r($ar);
    Copy after login
    Array(    [0] => Array        (            [0] => 2013-09-09 00:00:22            [1] => 13814002394            [2] => 内容        )    [1] => Array        (            [0] => 2013-09-09 00:00:19            [1] => 13814002394            [2] => 内容        )    [2] => Array        (            [0] => 2013-09-07 00:51:57            [1] => 15920531400            [2] => 内容        )    [3] => Array        (            [0] => 2013-09-06 22:21:02            [1] => 15920531400            [2] => 内容        )    [4] => Array        (            [0] => 2013-09-06 22:13:14            [1] => 15920531400            [2] => 内容        )    [5] => Array        (            [0] => 2013-09-06 22:07:42            [1] => 15920531400            [2] => 内容        )    [6] => Array        (            [0] => 2013-09-05 14:00:02            [1] => 15920531400            [2] => 内容        )    [7] => Array        (            [0] => 2013-09-05 12:10:13            [1] => 15920531400            [2] => 内容        )    [8] => Array        (            [0] => 2013-09-05 12:01:27            [1] => 15920531400            [2] => 内容        ))
    Copy after login

    还可以写作
    $r[0] = function(){return func_get_args();};$ar = call_user_func_array('array_map', $r);
    Copy after login
    更一般些

    这些结果都是二维数组啊,怎么样在前台循环出来!!!!!!!!!!!!!!!!!

    MVC模式!

    如果不用做其他计算,直接替换就行了,还要分数组么?
    preg_replace('/^\|\|1\|#\|([^\|]+)\|\|([^\|]+)\|\|(.+)$', '

  • $1    $2    $3
  • ', $return);

    可能要再去掉首尾行
    Related labels:
    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