Split Strings in Half (Word-Aware) with PHP

前端 未结 7 1600
醉话见心
醉话见心 2020-12-11 02:06

I\'m trying to split strings in half, and it should not split in the middle of a word.

So far I came up with the following which is 99% working :

$te         


        
7条回答
  •  甜味超标
    2020-12-11 02:52

    I was created a great solution, where we dont lost characters or Where the word is not cut wrongly.

    function split_title_middle ( $title ) {
        $title = strip_tags( $title );
        $middle_length = floor( strlen( $title ) / 2 );
        $new_title = explode( '
    ', wordwrap( $title, $middle_length, '
    ') ); if (isset( $new_title[2] ) ) { $new_title[1] .= ' ' . $new_title[2]; unset( $new_title[2] ); } return $new_title; } // how to use $title = split_title_middle( $title ); echo $title[0] . '' . $title[1] . '';

提交回复
热议问题