从 silverstripe 的内容中获取第一张图片

Getting first image from content in silverstripe

我有以下方法,它应该从内容中获取显示的第一张图像。

public function FirstImage($content)
{
    $dom = new DOMDocument();
    $dom->loadHTML($content);

    Debug::dump($content);

}

奇怪的是,当我执行 $dom->loadHTML 时,我收到以下错误 Empty string supplied as input,但一旦我转储它,我得到正确的数据。我该如何解决这个问题?

我也做了 $this->owner->Content 有同样的事情。

很抱歉,周末我的电脑上没有迟到的回复,我找到了一个更好的解决方案,然后通过 preg_match_all 使用 DOMDocument 实例。这是我使用过的以下代码。

    $pattern = '/<img[^>]+src[\s=\'"]';
    $pattern .= '+([^"\'>\s]+)/is';

    if (preg_match_all($pattern, $content, $match)) {
        $imageLink = preg_replace('/_resampled\/resizedimage[0-9]*-/', '', $match[1][0]);

        return (string) $imageLink;
    }