wp_extract_urls

The timeline below displays how wordpress function wp_extract_urls has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Uses RegEx to extract URLs from arbitrary content.
 *
 * @since 3.7.0
 * @since 6.0.0 Fixes support for HTML entities (Trac 30580).
 *
 * @param string $content Content to extract URLs from.
 * @return string[] Array of URLs found in passed string.
 */
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);
}

WordPress Version: 5.4

/**
 * Use RegEx to extract URLs from arbitrary content.
 *
 * @since 3.7.0
 *
 * @param string $content Content to extract URLs from.
 * @return string[] Array of URLs found in passed string.
 */
function wp_extract_urls($content)
{
    preg_match_all("#([\"']?)(" . '(?:([\w-]+:)?//?)' . '[^\s()<>]+' . '[.]' . '(?:' . '\([\w\d]+\)|' . '(?:' . "[^`!()\\[\\]{};:'\".,<>«»“”‘’\\s]|" . '(?:[:]\d+)?/?' . ')+' . ')' . ")\\1#", $content, $post_links);
    $post_links = array_unique(array_map('html_entity_decode', $post_links[2]));
    return array_values($post_links);
}

WordPress Version: 5.1

/**
 * Use RegEx to extract URLs from arbitrary content.
 *
 * @since 3.7.0
 *
 * @param string $content Content to extract URLs from.
 * @return array URLs found in passed string.
 */
function wp_extract_urls($content)
{
    preg_match_all("#([\"']?)(" . '(?:([\w-]+:)?//?)' . '[^\s()<>]+' . '[.]' . '(?:' . '\([\w\d]+\)|' . '(?:' . "[^`!()\\[\\]{};:'\".,<>«»“”‘’\\s]|" . '(?:[:]\d+)?/?' . ')+' . ')' . ")\\1#", $content, $post_links);
    $post_links = array_unique(array_map('html_entity_decode', $post_links[2]));
    return array_values($post_links);
}

WordPress Version: 4.1

/**
 * Use RegEx to extract URLs from arbitrary content.
 *
 * @since 3.7.0
 *
 * @param string $content Content to extract URLs from.
 * @return array URLs found in passed string.
 */
function wp_extract_urls($content)
{
    preg_match_all("#([\"']?)(" . "(?:([\\w-]+:)?//?)" . "[^\\s()<>]+" . "[.]" . "(?:" . "\\([\\w\\d]+\\)|" . "(?:" . "[^`!()\\[\\]{};:'\".,<>«»“”‘’\\s]|" . "(?:[:]\\d+)?/?" . ")+" . ")" . ")\\1#", $content, $post_links);
    $post_links = array_unique(array_map('html_entity_decode', $post_links[2]));
    return array_values($post_links);
}

WordPress Version: 4.0

/**
 * Use RegEx to extract URLs from arbitrary content.
 *
 * @since 3.7.0
 *
 * @param string $content Content to extract URLs from.
 * @return array URLs found in passed string.
 */
function wp_extract_urls($content)
{
    preg_match_all("#(" . "(?:([\\w-]+:)?//?)" . "[^\\s()<>]+" . "[.]" . "(?:" . "\\([\\w\\d]+\\)|" . "(?:" . "[^`!()\\[\\]{};:'\".,<>?«»“”‘’\\s]|" . "(?:[:]\\d+)?/?" . ")+" . ")" . ")#", $content, $post_links);
    $post_links = array_unique(array_map('html_entity_decode', $post_links[0]));
    return array_values($post_links);
}

WordPress Version: 3.7

/**
 * Use RegEx to extract URLs from arbitrary content
 *
 * @since 3.7.0
 *
 * @param string $content
 * @return array URLs found in passed string
 */
function wp_extract_urls($content)
{
    preg_match_all("#((?:[\\w-]+://?|[\\w\\d]+[.])[^\\s()<>]+[.](?:\\([\\w\\d]+\\)|(?:[^`!()\\[\\]{};:'\".,<>?«»“”‘’\\s]|(?:[:]\\d+)?/?)+))#", $content, $post_links);
    $post_links = array_unique(array_map('html_entity_decode', $post_links[0]));
    return array_values($post_links);
}