html class函数,wordpress函数sanitize_html_class()用法示例

sanitize_html_class( string $class, string $fallback = '' )

Sanitizes an HTML classname to ensure it only contains valid characters.

描述

Strips the string down to A-Z,a-z,0-9,_,-. If this results in an empty string then it will return the alternative value supplied.

参数

$class

(string)

(Required)

The classname to be sanitized

$fallback

(string)

(Optional)

The value to return if the sanitization ends up as an empty string. Defaults to an empty string.

Default value: ''

返回值

(string) The sanitized value

源代码

File: wp-includes/formatting.php

function sanitize_html_class( $class, $fallback = '' ) {

//Strip out any % encoded octets

$sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );

//Limit to A-Z,a-z,0-9,_,-

$sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );

if ( '' == $sanitized && $fallback ) {

return sanitize_html_class( $fallback );

}

/**

* Filters a sanitized HTML class string.

*

* @since 2.8.0

*

* @param string $sanitized The sanitized HTML class.

* @param string $class HTML class before sanitization.

* @param string $fallback The fallback string.

*/

return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );

}

更新日志

Version

描述

2.8.0

Introduced.

相关函数

Uses

wp-includes/formatting.php:

sanitize_html_class()

wp-includes/formatting.php:

sanitize_html_class

wp-includes/plugin.php:

apply_filters()

Used By

wp-admin/includes/class-wp-media-list-table.php:

WP_Media_List_Table::column_title()

wp-admin/includes/class-wp-press-this.php:

WP_Press_This::html()

wp-includes/link-template.php:

_navigation_markup()

wp-login.php:

login_header()

wp-admin/includes/class-wp-screen.php:

WP_Screen::add_help_tab()

wp-admin/includes/class-wp-plugin-install-list-table.php:

WP_Plugin_Install_List_Table::display_rows()

wp-admin/includes/template.php:

iframe_header()

wp-admin/includes/media.php:

attachment_submitbox_metadata()

wp-admin/menu-header.php:

_wp_menu_output()

wp-includes/formatting.php:

sanitize_html_class()

wp-includes/post-template.php:

get_post_class()

wp-includes/post-template.php:

get_body_class()

wp-includes/media.php:

gallery_shortcode()

wp-includes/media.php:

img_caption_shortcode()

wp-includes/comment-template.php:

get_comment_class()

wp-includes/class-wp-editor.php:

_WP_Editors::editor_settings()

Show 11 more used by

Hide more used by

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

Basic Example

// If you want to explicitly style a post, you can use the sanitized version of the post title as a class

$post_class = sanitize_html_class( $post->post_title );

echo '

';

?>

Created this function to help escape multiple HTML classes, you can give it an array of classes or a string of them separated by a delimiter:

if( ! function_exists("sanitize_html_classes") ){

function sanitize_html_classes($classes, $sep = " "){

$return = "";

if(!is_array($classes)) {

$classes = explode($sep, $classes);

}

if(!empty($classes)){

foreach($classes as $class){

$return .= sanitize_html_class($class) . " ";

}

}

return $return;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值