Split Strings in Half (Word-Aware) with PHP

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


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 :)


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"

Tags:

Php

String

Split