2016-07-21 4 views
2

私はPHPで正規表現を学ぼうとしています。なぜ、最終行の文字列変換エラーの配列になるのですか?どのように修正しますか?php regexは配列から文字列への変換エラーを起こす

$document = "{title}this is the title{/title}"; 
preg_match("/{title}(.*){\/title}/", $document, $match); 

echo $match; 
+2

'$ match'は配列で、' print_r($ match) 'を使用します –

+0

また、学習目的のために、regexを使って遊ぶのに便利なツールと、PHPでどのように動作するかはhttp://www.phpliveregexです。 com –

答えて

1

this demoを参照してください:

$document = "{title}this is the title{/title}"; 
preg_match("/{title}(.*){\/title}/", $document, $match); 
//echo $match; // PHP Notice: Array to string conversion in /home/jxkaKh/prog.php on line 5 
print_r($match); 
// => Array( [0] => {title}this is the title{/title} [1] => this is the title) 

preg_match referenceを参照してください:

int preg_match (string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]])
...
matches
matchesが提供されている場合、それは結果で満たされていますサーチ。 $matches[0]最初のキャプチャ括弧付きのサブパターンにマッチしたテキストを持つことになり、完全なパターンにマッチしたテキスト、$matches[1]が含まれている、などます。

パターンには、(.*)と定義されたキャプチャグループが含まれています。

関連する問題