Split Strings in Half (Word-Aware) with PHP

前端 未结 7 1583
醉话见心
醉话见心 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:45
    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

    0 讨论(0)
  • 2020-12-11 02:47

    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"
    
    0 讨论(0)
  • 2020-12-11 02:47

    I know this is an old question, but I have the following piece of code that should do what is needed.

    It by default it splits the string on the first occurrence of a space after the middle. If there are no spaces after the middle, it looks for the last space before the middle.

    function trim_text($input) {
        $middle = ceil(strlen($input) / 2);
        $middle_space = strpos($input, " ", $middle - 1);
    
        if ($middle_space === false) {
            //there is no space later in the string, so get the last sapce before the middle
            $first_half = substr($input, 0, $middle);
            $middle_space = strpos($first_half, " ");
        }
    
        if ($middle_space === false) {
            //the whole string is one long word, split the text exactly in the middle
            $first_half = substr($input, 0, $middle);
            $second_half = substr($input, $middle);
        }
        else {
            $first_half = substr($input, 0, $middle_space);
            $second_half = substr($input, $middle_space);
        }
            return array(trim($first_half), trim($second_half));
    }
    

    These are examples:

    Example 1:

    "WWWWWWWWWW WWWWWWWWWW WWWWWWWWWW WWWWWWWWWW"

    Is split as

    "WWWWWWWWWW WWWWWWWWWW"

    "WWWWWWWWWW WWWWWWWWWW"

    Example 2:

    "WWWWWWWWWWWWWWWWWWWW WWWWWWWWWW WWWWWWWWWW"

    Is split as

    "WWWWWWWWWWWWWWWWWWWW"

    "WWWWWWWWWW WWWWWWWWWW"

    Example 3:

    "WWWWWWWWWW WWWWWWWWWW WWWWWWWWWWWWWWWWWWWW"

    Is split as

    "WWWWWWWWWW WWWWWWWWWW"

    "WWWWWWWWWWWWWWWWWWWW"

    Example 4:

    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"

    Is split as

    "WWWWWWWWWWWWWWWWWWWW"

    "WWWWWWWWWWWWWWWWWWWW"

    Hope this can help someone out there :)

    0 讨论(0)
  • 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( '<br />', wordwrap( $title, $middle_length, '<br />') );
        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] . '<strong>' . $title[1] . '</strong>';
    
    0 讨论(0)
  • 2020-12-11 02:55
    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<$start;$i++){
                $string1 .= $text[$i]." ";
            }
            for($j=$start; $j<$end;$j++){
                $string2 .= $text[$j]." ";
            }           
        $result[] = $string1;
        $result[] = $string2;
        }
        else{
            $start = round($count/2)-1;
            $end = $count;
            for($i=0; $i<$start;$i++){
                $string1 .= $text[$i]." ";
            }
            for($j=$start; $j<$end;$j++){
                $string2 .= $text[$j]." ";
            }           
        $result[] = $string1;
        $result[] = $string2;
    
        }
    }
    else{
        $result[] = $string;
    }
    return $result;
    }
    

    Use this function to split string into half words..

    0 讨论(0)
  • 2020-12-11 03:00

    I tried using a couple of these answers but didn't get the best results so I thought I would share the solution. I wanted to split my titles in half and display one half white and one half green.

    I ended up combining word count, the length of the text, the half way point and a third of the string. This allowed me to make sure the white section is going to be somewhere between the third way/half way mark.

    I hope it helps someone. Be aware in my code -,& etc would be considered a word - it didn't cause me issues in my testing but I will update this if I find its not working as I hope.

    public function splitTitle($text){
    
        $words = explode(" ",$text);
        $strlen = strlen($text);
        $halfwaymark = ceil($strlen/2);
        $thirdwaymark = ceil($strlen/3);
    
        $wordcount = sizeof($words);
        $halfwords = ceil($wordcount/2);
        $thirdwords = ceil($wordcount/3);
    
        $string1 ='';
        $wordstep = 0;
        $wordlength = 0;
    
    
        while(($wordlength < $wordcount && $wordstep < $halfwords) || $wordlength < $thirdwaymark){
            $string1 .= $words[$wordstep].' ';
            $wordlength = strlen($string1);
            $wordstep++;
        }
    
        $string2 ='';
        $skipspace = 0;
        while(($wordstep < $wordcount)){
            if($skipspace==0) {
                $string2 .= $words[$wordstep];
            } else {
                $string2 .= ' '.$words[$wordstep];
            }
            $skipspace=1;
            $wordstep++;
        }
    
        echo $string1.' <span class="highlight">'.$string2.'</span>';
    
    }
    
    0 讨论(0)
提交回复
热议问题