wp_extract_urls( string $content )

Use RegEx to extract URLs from arbitrary content.


Description Description


Parameters Parameters

$content

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


Top ↑

Return Return

(array) URLs found in passed string.


Top ↑

Source 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( 'html_entity_decode', $post_links[2] ) );

	return array_values( $post_links );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    This Code:

    $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin elementum quis lacus in accumsan. Sed sed lacus odio. Sed ullamcorper, nibh et dignissim convallis, lacus tellus pellentesque ipsum, et interdum purus urna ultricies justo. Phasellus blandit eros nec lectus vestibulum consequat. Cras faucibus turpis sed ante commodo cursus. Duis vitae ligula vulputate, varius mi vel, facilisis est. Nulla id mollis ipsum. Nunc faucibus augue vel erat luctus sodales. Curabitur gravida vulputate nulla sed aliquam. Ut posuere mollis mauris, et placerat diam cursus vitae. Vivamus eros arcu, lobortis id sapien at, tempus tristique nunc. Praesent sollicitudin vulputate lorem, vitae vestibulum nisi pretium non. http://example.com is a cool site.';
    
    $urls = wp_extract_urls( $string );
    

    Will return an array like this:

    array( 0 => 'http://example.com' )
    

You must log in before being able to contribute a note or feedback.