<?php
$str = "姓名:
张二麻
年龄:
29
婚否:
未婚姓名:
李小四
年龄:
30
婚否:
已婚";print "<pre>";preg_match_all("/姓名:\s*([\S]+)/", $str, $arr);
print_r($arr[1]);preg_match_all("/年龄:\s*(\d+)/", $str, $arr);
print_r($arr[1]);preg_match_all("/婚否:\s*([\S]+)/", $str, $arr);
print_r($arr[1]);print "<pre>";?>
$str = "姓名:
张二麻
年龄:
29
婚否:
未婚姓名:
李小四
年龄:
30
婚否:
已婚";print "<pre>";preg_match_all("/姓名:\s*([\S]+)/", $str, $arr);
print_r($arr[1]);preg_match_all("/年龄:\s*(\d+)/", $str, $arr);
print_r($arr[1]);preg_match_all("/婚否:\s*([\S]+)/", $str, $arr);
print_r($arr[1]);print "<pre>";?>
实际的文档没有这么规则,有些段落可能是这样的。姓名:
张二麻
年龄:
29
婚否:
未婚
姓名:
李小四年龄:
30
婚否:
已婚再次麻烦,由于对正则不熟,再次摆脱,谢谢。
张二麻
年龄:
29
婚否:
未婚
姓名:
李小四年龄:
30
婚否:
已婚"一样适用的```````````
主要是还有这种情况,比如 李小四 的因为年龄不知道而其下一行空了,导致出了问题。