返回介绍

wp_html_excerpt()

发布于 2017-09-11 12:11:09 字数 2985 浏览 1060 评论 0 收藏 0

wp_html_excerpt( string $str,  int $count,  string $more = null )

Safely extracts not more than the first $count characters from html string.


description

UTF-8, tags and entities safe prefix extraction. Entities inside will NOT be counted as one character. For example & will be counted as 4, < as 3, etc.


参数

$str

(string) (Required) String to get the excerpt from.

$count

(int) (Required) Maximum number of characters to take.

$more

(string) (Optional) What to append if $str needs to be trimmed. Defaults to empty string.

Default value: null


返回值

(string) The excerpt.


源代码

File: wp-includes/formatting.php

function wp_html_excerpt( $str, $count, $more = null ) {
	if ( null === $more )
		$more = '';
	$str = wp_strip_all_tags( $str, true );
	$excerpt = mb_substr( $str, 0, $count );
	// remove part of an entity at the end
	$excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt );
	if ( $str != $excerpt )
		$excerpt = trim( $excerpt ) . $more;
	return $excerpt;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

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

Used By

  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::customize_register()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_author()
  • wp-includes/admin-bar.php: wp_admin_bar_site_menu()
  • wp-includes/comment.php: do_trackbacks()

User Contributed Notes

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

    Example

    
    $str = 'There are lots &amp; <strong>lots</strong> of usages for this function. I know you can think of some! (lots more text here)';
    
    echo wp_html_excerpt( $str, 50 );
    // Output: 'There are lots &amp; lots of usages for this funct'
    
    // Using the $more parameter:
    echo wp_html_excerpt( $str, 50, '...' );
    // Output: 'There are lots &amp; lots of usages for this funct...'
    

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

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

发布评论

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