wp_extract_urls

函数
wp_extract_urls ( $content )
参数
  • (string) $content Content to extract URLs from.
    Required:
返回值
  • (string[]) Array of URLs found in passed string.
定义位置
相关方法
wp_registration_urlwp_get_attachment_urlwp_insert_userwp_get_update_php_urlwp_guess_url
引入
3.7.0
弃用
-

wp_extract_urls: 这个函数用于从一个给定的字符串中提取所有的URLs。这对于解析包含链接的文本很有用,例如博客文章或评论: 该函数返回一个在输入字符串中发现的URL数组: 这个函数可以用来帮助在WordPress网站内寻找和链接到外部内容的过程自动化。

使用RegEx从任意的内容中提取URLs。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_extract_urls( $content ) {
preg_match_all(
"#(["']?)("
. '(?:([w-]+:)?//?)'
. '[^s()<>]+'
. '[.]'
. '(?:'
. '([wd]+)|'
. '(?:'
. "[^`!()[]{}:'".,<>«»“”‘’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 );
}
function wp_extract_urls( $content ) { preg_match_all( "#(["']?)(" . '(?:([w-]+:)?//?)' . '[^s()<>]+' . '[.]' . '(?:' . '([wd]+)|' . '(?:' . "[^`!()[]{}:'".,<>«»“”‘’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 ); }
function wp_extract_urls( $content ) {
	preg_match_all(
		"#(["']?)("
			. '(?:([w-]+:)?//?)'
			. '[^s()<>]+'
			. '[.]'
			. '(?:'
				. '([wd]+)|'
				. '(?:'
					. "[^`!()[]{}:'".,<>«»“”‘’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 );
}

常见问题

FAQs
查看更多 >