Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:59:57+00:00 2026-05-26T21:59:57+00:00

If I have sentences like this: $msg = hello how are you?are you fine?thanks..

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:59:58+00:00Added an answer on May 26, 2026 at 9:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a bunch of files that have sentences ending like this: \@.Next sentence
I have this function which cuts a string into a number of characters without
I have a sentence like this. 1 &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 3
I have a varargs constructor like this : public class Sentence { public String[]
I have a file like this: This is a sentence. This is another sentence.
I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I have a web application that translates sentences into English; the user chooses options
I have classes like this one: class SomeObject { public function __construct($param1, $param2) {
I have an android loyout like this: <ScrollView android:layout_width=fill_parent android:layout_height=fill_parent android:id=@+id/scrollview> <TextView android:id=@+id/text android:layout_width=wrap_content
I have some serial code like this that computes word concordances i.e. counting collocated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.