wp_extract_urls()
Use RegEx to extract URLs from arbitrary content.
Used By: do_enclose()
1 time — 0.000114 sec (fast) | 50000 times — 0.19 sec (very fast) | PHP 7.0.8, WP 4.6.1
No Hooks.
Return
String[]
. Array of URLs found in passed string.
Usage
wp_extract_urls( $content );
- $content(string) (required)
- Content to extract URLs from.
Examples
#1 Example of retrieving URLs from content
$content = 'Beginning of text with a link: http://wp-kama.com/ Continued, the link will now be in html <a href="http://wp-site.com/foo">link</a>. And another option, but now the path will be a link to the picture: <img alt=""" src="http://sitename.com/image.jpg">. That's it, that's enough for now.' $urls = wp_extract_urls( $content ); /* $urls will contain such an array: Array ( [0] => http://wp-kama.com/ [1] => http://wp-site.com/foo [2] => http://sitename.com/image.jpg ) */
#2 Doesn't work for localhost URLs without a TLD: [auto-translate]
$content = ' <a href="http://localhost.com:8889/?p=9">hi</a> <a href="http://localhost:8889/?p=9">hi</a> '; $urls = wp_extract_urls( $content ); /* $urls will contain such an array: Array ( [0] => http://localhost.com:8889/?p=9 ) */
See this ticket.
Changelog
Since 3.7.0 | Introduced. |
Since 6.0.0 | Fixes support for HTML entities (Trac 30580). |