wp_extract_urls() WordPress Function

The wp_extract_urls() function is a utility function that allows for the extraction of URLs from a given piece of text. This can be useful when trying to determine which links are being used in a given piece of content, or when trying to find all of the links in a given body of text.

wp_extract_urls( string $content ) #

Use RegEx to extract URLs from arbitrary content.


Parameters

$content

(string)(Required)Content to extract URLs from.


Top ↑

Return

(string[]) Array of URLs found in passed string.


Top ↑

Source

File: wp-includes/functions.php

function wp_extract_urls( $content ) {
	preg_match_all(
		"#([\"']?)("
			. '(?:([\w-]+:)?//?)'
			. '[^\s()<>]+'
			. '[.]'
			. '(?:'
				. '\([\w\d]+\)|'
				. '(?:'
					. "[^`!()\[\]{}:'\".,<>«»“”‘’\s]|"
					. '(?:[:]\d+)?/?'
				. ')+'
			. ')'
		. ")\\1#",
		$content,
		$post_links
	);

	$post_links = array_unique(
		array_map(
			static function( $link ) {
				// Decode to replace valid entities, like &amp;.
				$link = html_entity_decode( $link );
				// Maintain backward compatibility by removing extraneous semi-colons (`;`).
				return str_replace( ';', '', $link );
			},
			$post_links[2]
		)
	);

	return array_values( $post_links );
}


Top ↑

Changelog

Changelog
VersionDescription
6.0.0Fixes support for HTML entities (Trac 30580).
3.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More