0

preg_match_allを使用して特定の文字列間のテキストを取得する必要があります。交互に使用してみましたが、正しい情報が得られません。必要なのは、平日の文字列の間にランダムテキストを取得することです。金曜日からそれはテキストの終わりに行くべきです。

私のデータは次のとおりです。

Monday 1.1.
randomtext
randomtext
Tuesday 2.1.
randomtext
randomtext
Wednesday 3.1
randomtext
randomtext
Thusday 4.1.
randomtext
randomtext
randomtext
Friday 5.1
randomtext
randomtext

私が今持っているのはこれですが、火曜日に停止してから再度実行する必要がある月曜日以降にのみキャプチャします。

/(Monday|Tuesday|Wednesday|Thursday|Friday)([\s\S]+)/

何か案は?

4

3 に答える 3

0

これを試して、m修飾子と^+$を使用して、テキストの各行に一致させます。

$datas = <<<data
Monday 1.1.
randomtext
randomtext
Tuesday 2.1.
randomtext
randomtext
Wednesday 3.1
randomtext
randomtext
Thusday 4.1.
randomtext
randomtext
randomtext
Friday 5.1
randomtext
randomtext
data;
preg_match_all('#^(?!Monday|Tuesday|Wednesday|Thusday|Friday)\w+$#im',$datas,$matches);
print_r($matches);
于 2012-07-17T11:49:17.670 に答える
0

使用できますpreg_split()

$day_pattern = '/([a-z]+?day [\d\.]+\n)/i';
$bits = preg_split(
    $day_pattern,
    $str,
    null,
    PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
if ($bits)
    foreach($bits as $bit)
        echo preg_match($day_pattern, $bit)
            ?
            '<h3>'.$bit.'</h3>'
            :
            '<p>'.$bit.'</p>';

出力

<h3>Monday 1.1.</h3>
<p>randomtext
randomtext</p>
<h3>Tuesday 2.1.</h3>
<p>randomtext
randomtext</p>
<h3>Wednesday 3.1</h3>
<p>randomtext
randomtext</p>
<h3>Thusday 4.1.</h3>
<p>randomtext
randomtext
randomtext</p>
<h3>Friday 5.1</h3>
<p>randomtext
randomtext</p>
于 2012-07-17T12:01:40.213 に答える
0

あなたの例では、実際には「ランダムテキスト....」は特定の文字列の間にありません。これは、最後に文字列がないため、つまり金曜日の後です

Monday 1.1.
randomtext1
randomtext2
Tuesday 2.1.
randomtext3
randomtext4
Wednesday 3.1
randomtext5
randomtext6
Thursday 4.1.
randomtext7
randomtext8
randomtext9
Friday 5.1
randomtext10
randomtext11
Monday

テキストが上記のようになる場合、つまり、常に平日の文字列の間にある場合は、次のパターンを使用できます。

 (?<=Monday|Tuesday|Wednesday|Thursday|Friday)([\s\S]+?)(?=Monday|Tuesday|Wednesday|Thursday|Friday)
于 2012-07-17T12:47:51.633 に答える