Irene Ling
Irene Ling

Reputation: 1981

Split a long string not using space

If I have sentences like this:

$msg = "hello how are you?are you fine?thanks.."

and I wish to seperate it into 3 (or whatever number).

So I'm doing this:

    $msglen = strlen($msg);
    $seperate = ($msglen /3);

    $a = 0;
    for($i=0;$i<3;$i++)
    {
     $seperate = substr($msg,$a,$seperate)

     $a = $a + $seperate;
    }

So the output should be..

  1. hello how are
  2. [a space here->] you?are you [<-a space here]
  3. fine?thanks..

So is it possible to separate at middle of any word instead of having a space in front or end of the separated message?

Such as "thank you" -> "than" and "k you" instead of "thank" " you ". Because I'm doing a convert function and with a space in front or end it will effect the convertion , and the space is needed for the conversion,so I can't ignore or delete it.

Thanks.

Upvotes: 1

Views: 322

Answers (4)

Rahul Sekhar
Rahul Sekhar

Reputation: 2841

I take it you can't use trim because the message formed by the joined up strings must be unchanged?

That could get complicated. You could make something that tests for a space after the split, and if a space is detected, makes the split one character earlier. Fairly easy, but what if you have two spaces together? Or a single lettered word? You can of course recursively test this way, but then you may end up with split strings of lengths that are very different from each other.

You need to properly define the constraints you want this to function within.

Please state exactly what you want to do - do you want each section to be equal? Is the splitting in between words of a higher priority than this, so that the lengths do not matter much?

EDIT: Then, if you aren't worried about the length, you could do something like this [starting with Eriks code and proceeding to change the lengths by moving around the spaces:

$msg = "hello how are you?are you fine?thanks..";
$parts = split_without_spaces ($msg, 3);

function split_without_spaces ($msg, $parts) {
    $parts = str_split(trim($msg), ceil(strlen($msg)/$parts));
    /* Used trim above to make sure that there are no spaces at the start 
       and end of the message, we can't do anything about those spaces */

    // Looping to (count($parts) - 1) becaause the last part will not need manipulation
    for ($i = 0; $i < (count($parts) - 1) ; $i++ ) {

        $k = $i + 1;

        // Checking the last character of the split part and the first of the next part for a space
        if (substr($parts[$i], -1) == ' ' || $parts[$k][0] == ' ') {

            // If we move characters from the first part to the next:
            $num1 = 1;
            $len1 = strlen($parts[$i]);
            // Searching for the last two consecutive non-space characters
            while ($parts[$i][$len1 - $num1] == ' ' || $parts[$i][$len1 - $num1 - 1] == ' ') {
                $num1++;
                if ($len1 - $num1 - 2 < 0) return false;
            }

            // If we move characters from the next part to the first:
            $num2 = 1;
            $len2 = strlen($parts[$k]);
            // Searching for the first two consecutive non-space characters
            while ($parts[$k][$num2 - 1] == ' ' || $parts[$k][$num2] == ' ') {
                $num2++;
                if ($num2 >= $len2 - 1) return false;
            }

            // Compare to see what we can do to move the lowest no of characters
            if ($num1 > $num2) {
                $parts[$i] .= substr($parts[$k], 0, $num2);
                $parts[$k] = substr($parts[$k], -1 * ($len2 - $num2));
            }
            else {
                $parts[$k] = substr($parts[$i], -1 * ($num1)) . $parts[$k];
                $parts[$i] = substr($parts[$i], 0, $len1 - $num1);
            }
        }

    }

    return ($parts);
}

This takes care of multiple spaces and single lettered characters - however if they exist, the lengths of the parts may be very uneven. It could get messed up in extreme cases - if you have a string made up on mainly spaces, it could return one part as being empty, or return false if it can't manage the split at all. Please test it out thoroughly.

EDIT2: By the way, it'd be far better for you to change your approach in some way :) I seriously doubt you'd actually have to use a function like this in practice. Well.. I hope you do actually have a solid reason to, it was somewhat fun coming up with it.

Upvotes: 1

JRL
JRL

Reputation: 77995

You're probably looking for str_split, chunk_split or wordwrap.

Upvotes: 0

Erik Ekman
Erik Ekman

Reputation: 2066

If you want to split the string into exact thirds it is not known where the cut will be, maybe in a word, maybe between words.

Your code can be simplified to:

$msg = "hello how are you?are you fine?thanks..";
$parts = str_split($msg, ceil(strlen($msg)/3));

Note that ceil() is needed, otherwise you might get 4 elements out because of rounding.

Upvotes: 0

Christopher
Christopher

Reputation: 774

If you simply want to eliminate leading and trailing spaces, consider trim to be used on each result of your split.

Upvotes: 0

Related Questions