Split Strings in Half (Word-Aware) with PHP

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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 :

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog"; $half = (int)ceil(count($words = str_word_count($text, 1)) / 2);  $string1 = implode(' ', array_slice($words, 0, $half)); $string2 = implode(' ', array_slice($words, $half)); 

This does work, correctly splitting any string in half according to the number of words in the string. However, it is removing any symbols in the string, for example for the above example it would output :

The Quick Brown Fox Jumped Over The Lazy Dog 

I need to keep all the symbols like : and / in the string after being split. I don't understand why the current code is removing the symbols... If you can provide an alternative method or fix this method to not remove symbols, it would be greatly appreciated :)

回答1:

Upon looking at your example output, I noticed all our examples are off, we're giving less to string1 if the middle of the string is inside a word rather then giving more.

For example the middle of The Quick : Brown Fox Jumped Over The Lazy / Dog is The Quick : Brown Fox Ju which is in the middle of a word, this first example gives string2 the split word; the bottom example gives string1 the split word.

Give less to string1 on split word

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";  $middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;  $string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox " $string2 = substr($text, $middle);  // "Jumped Over The Lazy / Dog" 

Give more to string1 on split word

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";  $splitstring1 = substr($text, 0, floor(strlen($text) / 2)); $splitstring2 = substr($text, floor(strlen($text) / 2));  if (substr($splitstring1, 0, -1) != ' ' AND substr($splitstring2, 0, 1) != ' ') {     $middle = strlen($splitstring1) + strpos($splitstring2, ' ') + 1; } else {     $middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;     }  $string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox Jumped " $string2 = substr($text, $middle);  // "Over The Lazy / Dog" 


回答2:

function split_half($string, $center = 0.4) {         $length2 = strlen($string) * $center;         $tmp = explode(' ', $string);         $index = 0;          $result = Array('', '');         foreach($tmp as $word) {             if(!$index && strlen($result[0]) > $length2) $index++;             $result[$index] .= $word.' ';         }         return $result; } 

Demo: http://codepad.viper-7.com/I58gcI



回答3:

Just change the line:

$half = (int)ceil(count($words = (count(explode(" ",$text))) / 2); 

str_word_count() may not count : or / as word.



回答4:

function split_half($string){ $result= array(); $text = explode(' ', $string); $count = count($text); $string1 = ''; $string2 = ''; if($count > 1){     if($count % 2 == 0){         $start = $count/2;         $end = $count;         for($i=0; $i

Use this function to split string into half words..



回答5:

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] . '';


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!