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.
Return
(string[]) Array of URLs found in passed string.
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 &.
$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 );
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 6.0.0 | Fixes support for HTML entities (Trac 30580). |
| 3.7.0 | Introduced. |