仅获取锚标记href值并将其放入数组

时间:2019-02-10 15:43:55

标签: php

<ul class="wp-block-gallery columns-4 is-cropped">
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_01.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_01.jpg" alt="" data-id="322" data-link="http://lei.local/gallery/image_01/" class="wp-image-322"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_02.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_02.jpg" alt="" data-id="323" data-link="http://lei.local/gallery/image_02/" class="wp-image-323"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_03.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_03.jpg" alt="" data-id="324" data-link="http://lei.local/gallery/image_03/" class="wp-image-324"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_04.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_04.jpg" alt="" data-id="325" data-link="http://lei.local/gallery/image_04/" class="wp-image-325"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_05-1.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_05-1.jpg" alt="" data-id="326" data-link="http://lei.local/gallery/image_05-1/" class="wp-image-326"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_06-1.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_06-1.jpg" alt="" data-id="327" data-link="http://lei.local/gallery/image_06-2/" class="wp-image-327"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_07.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_07.jpg" alt="" data-id="328" data-link="http://lei.local/gallery/image_07/" class="wp-image-328"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_08.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_08.jpg" alt="" data-id="329" data-link="http://lei.local/gallery/image_08/" class="wp-image-329"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_09.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_09.jpg" alt="" data-id="330" data-link="http://lei.local/gallery/image_09/" class="wp-image-330"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_10.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_10.jpg" alt="" data-id="331" data-link="http://lei.local/gallery/image_10/" class="wp-image-331"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_11.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_11.jpg" alt="" data-id="332" data-link="http://lei.local/gallery/image_11/" class="wp-image-332"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_12.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_12.jpg" alt="" data-id="333" data-link="http://lei.local/gallery/image_12/" class="wp-image-333"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_13.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_13.jpg" alt="" data-id="334" data-link="http://lei.local/gallery/image_13/" class="wp-image-334"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_14.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_14.jpg" alt="" data-id="335" data-link="http://lei.local/gallery/image_14/" class="wp-image-335"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_15.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_15.jpg" alt="" data-id="336" data-link="http://lei.local/gallery/image_15/" class="wp-image-336"></a></figure></li>
    <li class="blocks-gallery-item"><figure><a href="http://lei.local/wp-content/uploads/2019/02/image_16.jpg"><img src="http://lei.local/wp-content/uploads/2019/02/image_16.jpg" alt="" data-id="337" data-link="http://lei.local/gallery/image_16/" class="wp-image-337"></a></figure></li>
</ul>

我只想从该内容中的每个锚标记中获取href值,然后将其放入数组中。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用dom crawler symfony库。确保您有作曲家 要安装它composer require symfony/http-foundation symfony/dom-crawler。有关更多详细信息,Dom crawler symfony Symfony HTTP Foundation

<?php
require_once '/path/to/vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER
);

$request->query->get('link point to your get file');

$html = $request->getContent();

$links = [];

$crawler = new Crawler($html);

$anchors = $crawler->filter('body > a');

foreach ($anchors as $a) {
   $links[] = $a->href;
}

var_dump($links);