簡單地說:
$matches = array();
preg_match_all('/^(?P<propname>[^:]+):\s*(?P<propval>.*)$/m', $string, $matches, PREG_SET_ORDER);
print_r($matches);
此正則表達式的冒號之前匹配任何東西,抓住它propname
,其餘的行年底將在propval
被捕獲。 產生的結構將
Array
(
[0] => Array
(
[0] => Name : John Thompson
[propname] => Name
[1] => Name
[propval] => John Thompson
[2] => John Thompson
)
[1] => Array
(
[0] => Address : 123 street abc state xyz country
[propname] => Address
[1] => Address
[propval] => 123 street abc state xyz country
[2] => 123 street abc state xyz country
)
)
由於這是一個簡單的正則表達式,我認爲,你不熟悉正則表達式,所以我強烈建議閱讀J.F. Friedl - Mastering Regular expressions,你很快就會易工藝完美演繹:-)