Yoast\WP\SEO\Images\Application

Image_Content_Extractor::gather_images()publicYoast 1.0

Gathers all images from content.

Метод класса: Image_Content_Extractor{}

Возвращает

int[]. An associated array of image IDs, keyed by their URLs.

Использование

$Image_Content_Extractor = new Image_Content_Extractor();
$Image_Content_Extractor->gather_images( $content );
$content(строка) (обязательный)
The content.

Код Image_Content_Extractor::gather_images() Yoast 24.0

public function gather_images( $content ) {

	/**
	 * Filter 'wpseo_force_creating_and_using_attachment_indexables' - Filters if we should use attachment indexables to find all content images. Instead of scanning the content.
	 *
	 * The default value is false.
	 *
	 * @since 21.1
	 */
	$should_not_parse_content = \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false );
	/**
	 * Filter 'wpseo_force_skip_image_content_parsing' - Filters if we should force skip scanning the content to parse images.
	 * This filter can be used if the regex gives a faster result than scanning the code.
	 *
	 * The default value is false.
	 *
	 * @since 21.1
	 */
	$should_not_parse_content = \apply_filters( 'wpseo_force_skip_image_content_parsing', $should_not_parse_content );

	if ( ! $should_not_parse_content && \class_exists( WP_HTML_Tag_Processor::class ) ) {
		return $this->gather_images_wp( $content );
	}

	if ( ! $should_not_parse_content && \class_exists( DOMDocument::class ) ) {

		return $this->gather_images_DOMDocument( $content );
	}

	if ( \strpos( $content, 'src' ) === false ) {
		// Nothing to do.
		return [];
	}

	$images = [];
	$regexp = '<img\s[^>]*src=("??)([^" >]*?)\\1[^>]*>';
	// Used modifiers iU to match case insensitive and make greedy quantifiers lazy.
	if ( \preg_match_all( "/$regexp/iU", $content, $matches, \PREG_SET_ORDER ) ) {
		foreach ( $matches as $match ) {
			$images[ $match[2] ] = 0;
		}
	}

	return $images;
}