嘗試這樣:
$text = "attr1=\"some text\" attr2 = \"some other text\" attr3= \"some weird [email protected]'#$\\\"=+ text\"";
echo $text;
preg_match_all('/(\S+)\s*=\s*"((?:\\\\.|[^\\"])*)"/', $text, $matches, PREG_SET_ORDER);
print_r($matches);
主要生產:
attr1="some text" attr2 = "some other text" attr3= "some weird [email protected]'#$\"=+ text"
Array
(
[0] => Array
(
[0] => attr1="some text"
[1] => attr1
[2] => some text
)
[1] => Array
(
[0] => attr2 = "some other text"
[1] => attr2
[2] => some other text
)
[2] => Array
(
[0] => attr3= "some weird [email protected]'#$\"=+ text"
[1] => attr3
[2] => some weird [email protected]'#$\"=+ text
)
)
和簡短說明:
(\S+) // match one or more characters other than white space characters
// > and store it in group 1
\s*=\s* // match a '=' surrounded by zero or more white space characters
" // match a double quote
( // open group 2
(?:\\\\.|[^\\"])* // match zero or more sub strings that are either a backslash
// > followed by any character, or any character other than a
// > backslash
) // close group 2
" // match a double quote
你不是解析的標記語言,對? – 2009-10-22 07:50:58
很高興問這個!不,只需編寫我自己的語法,便於在命令行上鍵入。 – dreeves 2009-10-22 07:57:51
「很容易在命令行上鍵入」,那麼你可能會對http://docs.php.net/getopt – VolkerK 2009-10-22 09:31:27