当前位置:Gxlcms > PHP教程 > 返回字符串截取循环

返回字符串截取循环

时间:2021-07-01 10:21:17 帮助过:8人阅读

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 内容

  • 这样的循环方式!

    1) 		{			$new_arr[]=explode('|',$value);		}	}	echo "
    ";	print_R($new_arr);	echo "
    ";?>
    简单的写了下,凑合着能,不知道有没有什么更好的办法

    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] => 内容


    )这是打印出来的效果

    $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);
    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] => 内容        ))

    已经可以用了
    如果想把对应的放在一起就
    $ar = array_map(function($a,$b,$c){return array($a,$b,$c);}, $r[1], $r[2], $r[3]);print_r($ar);
    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] => 内容        ))

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

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

    MVC模式!

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

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

    可能要再去掉首尾行

    人气教程排行