wp_extract_urls( string $content ): string[]
Uses RegEx to extract URLs from arbitrary content.
Parameters
-
$content
string Required -
Content to extract URLs from.
Return
string[] Array of URLs found in passed string.
Source
File: wp-includes/functions.php
.
View all references
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 &.
$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 );
}
Changelog
Version | Description |
---|---|
6.0.0 | Fixes support for HTML entities (Trac 30580). |
3.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
This doesn’t work for localhost URLs without TLDs:
(See this ticket.)
Example
This Code:
Will return an array like this: