返回介绍

make_clickable()

发布于 2017-09-11 01:43:55 字数 3967 浏览 960 评论 0 收藏 0

make_clickable( string $text )

Convert plaintext URI to HTML links.


description

Converts URI, www and ftp, and email addresses. Finishes by fixing links within links.


参数

$text

(string) (Required) Content to convert URIs.


返回值

(string) Content with converted URIs.


源代码

File: wp-includes/formatting.php

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 <pre> or <code>
	foreach ( $textarr as $piece ) {

		if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) || preg_match( '|^<script[\s>]|i', $piece ) || preg_match( '|^<style[\s>]|i', $piece ) )
			$nested_code_pre++;
		elseif ( $nested_code_pre && ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) || '</script>' === strtolower( $piece ) || '</style>' === strtolower( $piece ) ) )
			$nested_code_pre--;

		if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
			$r .= $piece;
			continue;
		}

		// Long strings might contain expensive edge cases ...
		if ( 10000 < 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 = '~
				([\\s(<.,;:!?])                                       

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/formatting.php: _split_str_by_whitespace()
  • wp-includes/formatting.php: make_clickable()

Used By

  • wp-includes/formatting.php: make_clickable()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Nicola Mustone

    This example shows how to use make_clickable with a string of text to turn any URLs within into links.

    
    <?php echo make_clickable( '<p>To find out more about this function, visit this link: https://developer.wordpress.org/reference/functions/make_clickable/</p>' ); ?>
    

    The result will be:

    
    <p>To find out more about this function, visit this link: <a href="https://developer.wordpress.org/reference/functions/make_clickable/" rel="nofollow">https://developer.wordpress.org/reference/functions/make_clickable/</a></p>
    
  2. Display all URLs in clickable links

    
    $string = "This is a long text that contains some links like http://www.wordpress.org and http://www.wordpress.com .";
    
    echo make_clickable( $string ); 
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文