1

私はこれを盲目的に見つめています。preg_match_allを使用してハッシュの間に配置されたテキストを見つけようとしています。

##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##

私が何をしようとしても、あるときは取得of the textし、別のときはハッシュの合間にのみ、誰かが正規表現を手伝ってくれるでしょうか?

4

3 に答える 3

1
$str = '##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##';

preg_match_all( '~##(.+)##~', $str, $matches );

print_r($ matches)は次のようになります:

Array ( [0] => START of the text [1] => END of the the text [2] => START of the text 2 [3] => END of the the text 2 ) 
于 2013-01-23T08:48:07.713 に答える
1
$string = '##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##';

preg_match_all('/##START.+?##[\n](.+?)##END/s', $string, $matches);
// look for any characters between ##START and ##END, 's' will make it match newlines as well

print_r($matches[1]);

出力します:

Array
(
    [0] => the text <b>starts</b> here etc

    [1] => Other version stringtext <b>starts</b> here etc

)
于 2013-01-22T20:40:43.937 に答える
0

私は正規表現があまり得意ではありませんが、これが私の解決策です。ハッシュで始まる行を一致させていません。^は行開始用です。(?!#)は負の先読みで、#文字を探しています。ライン。

$string = <<<EOD
##START of the text##
the text <b>starts</b> here etc
##END of the the text##
##START of the text 2##
Other version stringtext <b>starts</b> here etc
##END of the the text 2##
EOD;

preg_match_all('~^(?!#).*~m',$string,$match);
echo "<pre>";
print_r($match);

私が言ったように、私は正規表現に慣れていないので、私が間違っている場合は専門家が私に警告します。

于 2013-01-22T21:36:03.613 に答える