PHP 7.4 : DOMDocument クラスを利用して、HTMLの要素を取得する

ドキュメント

https://www.php.net/manual/ja/class.domdocument.php

環境

作業メモ

// in Dockerfile
FROM php:7.4-fpm-alpine

RUN set -eux \
    && apk update && apk upgrade \
    && apk add php7-xml \
// in Test
        // Assert HTML with DOMDocument
        $dom = new \DOMDocument();
        libxml_use_internal_errors( true );
        $dom->loadHTML($response->content());
        $title = $dom->getElementsByTagName('title')->item(0)->textContent;
        $this->assertEquals('Sample App', $title);