0

このテキスト文字列を取得するための解決策を必死に探しています

<h6>First pane</h6>
... pane content ...
<h6>Second pane</h6>
Hi, this is a comment.
To delete a comment, just log in and view the post's comments.
There you will have the option to edit
or delete them.
<h6>Last pane</h6>
... last pane content ...

PHP 配列に解析されます。

私はそれを分離する必要があります

1.
1.0=> First pane
1.1=> ... pane content ... 

2.
2.0=> Second pane
2.1=> Hi, this is a comment.
    To delete a comment, just log in and view the post's comments.
    There you will have the option to edit
    or delete them.

3.
3.0=> Last pane
3.1=> ... last pane content ...
4

3 に答える 3

1

正規表現は次のようになります。

/<h6>([^<]+)<\/h6>([^<]+)/im

次のスクリプトを実行すると、探している値が $matches[1] と $matches[2] にあることがわかります。

$s = "<h6>First pane</h6>
... pane content ...
<h6>Second pane</h6>
Hi, this is a comment.
To delete a comment, just log in and view the post's comments.
There you will have the option to edit
or delete them.
<h6>Last pane</h6>
... last pane content ..";
$r = "/<h6>([^<]+)<\/h6>([^<]+)/im";

$matches = array();
preg_match_all($r,$s,$matches);

print_r($matches);
于 2010-12-03T16:23:13.420 に答える
1

正規表現で HTML を解析しようとしないでください。これは、非常に単純な HTML を除いて、多くの苦痛と不幸を引き起こす運命にあり、ドキュメント構造の何かが変更されると即座に壊れます。代わりに、php のDOMDocument http://php.net/manual/en/class.domdocument.phpなどの適切な HTML または DOM パーサーを使用してください。

たとえば、 getElementsByTagName http://www.php.net/manual/en/domdocument.getelementsbytagname.phpを使用して、すべてh6のを取得できます。

于 2010-12-03T16:24:19.957 に答える
0

PREG_SET_ORDER フラグが探しているものだと思います。

$regex = '~<h6>([^<]+)</h6>\s*([^<]+)~i';

preg_match_all($regex, $source, $matches, PREG_SET_ORDER);

このように、$matches 配列の各要素は、全体的な一致とそれに続く 1 回の一致試行のすべてのグループ キャプチャを含む配列になります。最初の試合までの結果は次のようになります。

配列
(
    [0] => 配列
        (
            [0] => 最初のペイン
... ペインの内容 ...

            [1] => 最初のペイン
            [2] => ... ペインの内容 ...

        )

ideone での動作をご覧ください

編集:\s*私も追加したことに注意してください。それがないと、一致したコンテンツは常に行区切りなしで始まります。

于 2010-12-03T17:23:01.790 に答える