1. 首页
  2. WordPress 函数手册

make_clickable()

make_clickable( string $text )

将纯文本URI转换为HTML链接。
Convert plaintext URI to HTML links.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

转换URI、www和ftp以及电子邮件地址。通过固定链接内的链接完成。


参数(Parameters)

参数 类型 必填 说明
$text (string) 必需 要转换uri的内容。

返回(Return)

(string)具有转换的uri的内容。


源码(Source)

/**
 * Convert plaintext URI to HTML links.
 *
 * Converts URI, www and ftp, and email addresses. Finishes by fixing links
 * within links.
 *
 * @since 0.71
 *
 * @param string $text Content to convert URIs.
 * @return string Content with converted URIs.
 */
function make_clickable( $text ) {
	$r = '';
	$textarr = preg_split( '/(<><>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
	$nested_code_pre = 0; // Keep track of how many levels link is nested inside /* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
 or 
	foreach ( $textarr as $piece ) {

		if ( preg_match( '|^]|i', $piece ) || preg_match( '|^]|i', $piece ) )
			$nested_code_pre++;
		elseif ( ( '' === strtolower( $piece ) || '
' === strtolower( $piece ) ) && $nested_code_pre )
			$nested_code_pre--;
		if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' &&="" !="" preg_match(="">< strlen(="" $piece="" )="" )="" {="" ...="" break="" it="" up="" foreach="" (="" _split_str_by_whitespace(="" $piece,="" 2100="" )="" as="" $chunk="" )="" {="" 2100:="" extra="" room="" for="" scheme="" and="" leading="" and="" trailing="" paretheses="" if="" (="" 2101="">< strlen(="" $chunk="" )="" )="" {="" $r="" .="$chunk;" too="" big,="" no="" whitespace:="" bail.="" }="" else="" {="" $r="" .="make_clickable(" $chunk="" );="" }="" }="" }="" else="" {="" $ret=" $piece " ;="" pad="" with="" whitespace="" to="" simplify="" the="" regexes="" $url_clickable='~
				(&#91;\s(<.,;:!?&#93;)                                        # 1: Leading whitespace, or punctuation
				(                                                      # 2: URL
					&#91;\w&#93;{1,20}+://                                # Scheme and hier-part prefix
					(?=S{1,2000}s)                               # Limit to URLs less than about 2000 characters long
					&#91;\w\x80-\xff#%\~/@\&#91;\&#93;*(+=&$-&#93;*+         # Non-punctuation URL character
					(?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
						&#91;' .,;:!?)&#93;="" #="" punctuation="" url="" character="" &#91;\w\x80-\xff#%\~/@\&#91;\&#93;*(+="&$-&#93;++" #="" non-punctuation="" url="" character="" )*="" )="" ()?)="" #="" 3:="" trailing="" closing="" parenthesis="" (for="" parethesis="" balancing="" post="" processing)="" ~xs';="" the="" regex="" is="" a="" non-anchored="" pattern="" and="" does="" not="" have="" a="" single="" fixed="" starting="" character.="" tell="" pcre="" to="" spend="" more="" time="" optimizing="" since,="" when="" used="" on="" a="" page="" load,="" it="" will="" probably="" be="" used="" several="" times.="" $ret="preg_replace_callback(" $url_clickable,="" '_make_url_clickable_cb',="" $ret="" );="" $ret="preg_replace_callback(" '#(&#91;s="">])((www|ftp).[w\x80-\xff#$%&~/.-;:=,?@[]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
			$ret = preg_replace_callback( '#([s>])([.0-9a-z_+-]+)@(([0-9a-z-]+.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
			$ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
			$r .= $ret;
		}
	}
	// Cleanup of accidental links within links
	return preg_replace( '#(]+?>|>))]+?>([^>]+?)#i', "$1$3", $r );
}
更新版本 源码位置 使用 被使用
0.71 wp-includes/formatting.php:2929 1 function 2

笔记(Notes)

这个例子展示了如何使用make_clickable和一个文本字符串来将其中的任何url转换为链接。
显示可点击链接中的所有URL

make_clickable() 为WP2原创文章,链接:https://www.wp2.cn/functions/make_clickable/

发表评论

您的电子邮箱地址不会被公开。