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
檢視更多 >