3

一連の文字列からデータを抽出しようとしていますが、うまくいきません。以下のサンプル コードでは、preg_split を使用してみましたが、必要な結果が得られません。

以下のコードを使用します。

<?php
$str = '<a href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer">Nike Air Jordan SC-2 Mens Basketball Shoes 454050-035</a><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />
';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);

echo '<pre>';
print_r($chars);
echo '<pre>';
?>

結果を与える:

Array
(
    [0] => Array
        (
            [0] =>  0
        )

    [1] => Array
        (
            [0] => href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer">Nike
            [1] => 3
        )

    [2] => Array
        (
            [0] => Air
            [1] => 167
        )

    [3] => Array
        (
            [0] => Jordan
            [1] => 171
        )

    [4] => Array
        (
            [0] => SC-2
            [1] => 178
        )

    [5] => Array
        (
            [0] => Mens
            [1] => 183
        )

    [6] => Array
        (
            [0] => Basketball
            [1] => 188
        )

    [7] => Array
        (
            [0] => Shoes
            [1] => 199
        )

    [8] => Array
        (
            [0] => 454050-035 205
        )

    [9] => Array
        (
            [0] => src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA"
            [1] => 224
        )

    [10] => Array
        (
            [0] => width="1"
            [1] => 305
        )

    [11] => Array
        (
            [0] => height="1"
            [1] => 315
        )

    [12] => Array
        (
            [0] => border="0"
            [1] => 326
        )

    [13] => Array
        (
            [0] => alt=""
            [1] => 337
        )

    [14] => Array
        (
            [0] => style="border:none
            [1] => 344
        )

    [15] => Array
        (
            [0] => !important;
            [1] => 363
        )

    [16] => Array
        (
            [0] => margin:0px
            [1] => 375
        )

    [17] => Array
        (
            [0] => !important;"
            [1] => 386
        )

    [18] => Array
        (
            [0] => />

            [1] => 399
        )

)

array1 の「Nike is included when I only need is just URL.

[1] => Array
        (
            [0] => href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer">Nike
            [1] => 3
        )

実際、 $str を抽出する私の最終的な目標は、次のようにソース URL とアンカー テキストを別の配列に出力することです。

URL:

http://www.amazon.com/gp/product/B008EYEYBA/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B008EYEYBA&linkCode=as2&tag=mytwitterpage-20

アンカーテキスト:

ナイキ エア ジョーダン SC-2 メンズ バスケットボール シューズ 454050-035

これをどのように達成できるかについてのアイデアは大歓迎です。

4

2 に答える 2

2

正規表現を使用して html を解析することは、悪い習慣です。PHP にはそのためのDOM拡張機能があります。遭遇する可能性のあるすべてのhtmlで機能するユニバーサル正規表現を構築することはできません。DOM アプローチは、はるかに拡張可能です。

$string = '<a href="https://rads.stackoverflow.com/amzn/click/B008EYEYBA">Nike Air Jordan SC-2 Mens Basketball Shoes 454050-035</a><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($string);
libxml_clear_errors();
$elementA = $dom->getElementsByTagName('a')->item(0);
$aText = $elementA->nodeValue;
$aLink = $elementA->getAttribute('href');
echo $aLink . "\n" . $aText;
于 2012-11-23T13:04:03.923 に答える
-1

これは、php 関数を使用して行うことができます。

ここでアンカー タグを削除します。

strip_tags() 関数を使用してすべてのタグを削除できます。

于 2012-11-23T12:36:28.133 に答える