1

テキストの塊で 2 つのタグを見つけ、それらの間にあるテキストを保持する必要があります。

たとえば、「Begin」タグが-----start-----「End」タグが-----end-----

このテキストを考えると:

rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r
-----end-----gcgkhjkn

2 つのタグの間のテキストだけを保持する必要があります。isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r

何か案は?ありがとうございました。

4

3 に答える 3

12

いくつかの方法を次に示します。

$lump = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$start_tag = '-----start-----';
$end_tag = '-----end-----';

// method 1
if (preg_match('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    echo $matches[1];
}

// method 2 (faster)
$startpos = strpos($lump, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
    $endpos = strpos($lump, $end_tag, $startpos);
    if ($endpos !== false) {
        echo substr($lump, $startpos, $endpos - $startpos);
    }
}

// method 3 (if you need to find multiple occurrences)
if (preg_match_all('/'.preg_quote($start_tag).'(.*?)'.preg_quote($end_tag).'/s', $lump, $matches)) {
    print_r($matches[1]);
}
于 2012-06-30T21:02:39.923 に答える
7

これを試して:

$start = '-----start-----';
$end   = '-----end-----';
$string = 'rtyfbytgyuibg-----start-----isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r-----end-----gcgkhjkn';
$output = strstr( substr( $string, strpos( $string, $start) + strlen( $start)), $end, true);
echo $output;

これは印刷されます

isnv4b987b6vdc5y6ughnjmn9b8v76ctyubinn98b76r
于 2012-06-30T21:01:35.940 に答える