Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为标题中的每个字母、空格和字符添加一个span标记_Arrays - Fatal编程技术网

Arrays 为标题中的每个字母、空格和字符添加一个span标记

Arrays 为标题中的每个字母、空格和字符添加一个span标记,arrays,Arrays,我尝试将span标记添加到标题中的每个字母、空格和字符中。当我使用stru split时,特殊字符被转换成另一种格式 if ( !function_exists( 'my_title' ) ): function my_title( $str = '' ) { $output = ''; $str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str; if ( emp

我尝试将span标记添加到标题中的每个字母、空格和字符中。当我使用
stru split
时,特殊字符被转换成另一种格式

if ( !function_exists( 'my_title' ) ):
    function my_title( $str = '' ) {
        $output = '';
        $str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;

        if ( empty( $str ) ) {
            return $output;
        }

        $i = 1;
        foreach( str_split ( $str ) as $letter ) if ( $i++ <= 12 ) {
            if ( !empty( $letter ) ) {
                $output .= '<span>' . $letter . '</span>';
            }
        };

        return wp_kses_post( $output );

    }
endif;

echo my_title('Page title - with dash');
如果(!function_存在('my_title')):
函数my_title($str=''){
$output='';
$str=空($str)?wp_strip_all_标记(获取_title()):$str;
如果(空($str)){
返回$output;
}
$i=1;

foreach(str_split($str)as$letter)if($i++问题可能是因为应用于文章标题的函数,其中某些字符(如
-
(破折号))会自动转换为HTML实体,如
&
,用于“漂亮”破折号

试试这个,对我来说很有用:

if ( !function_exists( 'my_title' ) ):
    function my_title( $str = '', $max_chars = 12 ) {
        $output = '';

        remove_filter( 'the_title', 'wptexturize' );
        $str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;
        add_filter( 'the_title', 'wptexturize' );

        if ( empty( $str ) ) {
            return $output;
        }

        $str = html_entity_decode( $str, ENT_NOQUOTES, 'UTF-8' );
        for ( $i = 0; $i < min( mb_strlen( $str ), $max_chars ); $i++ ) {
            if ( $letter = mb_substr( $str, $i, 1 ) ) {
                $output .= '<span>' . $letter . '</span>';
            }
        }

        return wp_kses_post( $output );

    }
endif;
为此:

$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;

i、 e.没有必要删除
wptorysize
钩子。如果您想保留
wptorysize()
函数所转换的“漂亮”破折号和其他特殊字符,=)

var\u dump(str\u split('Page title-with-dash')的输出是什么
?特殊字符被转换为另一种格式。-那是什么格式?我测试了你的代码,它工作得很好-我得到了
第ti页…
“-”被转换为“̵;”,所以每个字符在span标记中是分开的。
$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;