WordPress函数:get_url_in_content 从文章内容中提取第一个链接

追格官方小助手/ 2022年06月26日/ WordPress/ 浏览 962

函数 get_url_in_content 的作用就是从html内容中提取第一个链接。


函数源码:


function get_url_in_content( $content ) {
	if ( empty( $content ) ) {
		return false;
	}

	if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
		return esc_url_raw( $matches[2] );
	}

	return false;
}


使用举例:


$url = get_url_in_content($post->post_content);


发表评论

暂无评论,抢个沙发...

客服 工单