split text into two character sequence

Suppose this is a sentence:           "php is good"

Above sentence should break into like this:
ph
hp
p {space}
{space} i
is
s {space}
{space} g
go
oo
od

I assume the code will be like this:
<?php
function BreakSentence($text) {
$two = array();
    $length =  mb_strlen($text);
    $x = 0; $y = 1;
    for($i=1;$i<=$length;$i++) {
        $A1 = $x+1;
        $A2 = $y+1;
        $char2 = ($text[$A1].' '.$text[$A2]);
        array_push($two, $char2);
    }
    return $two;
}
?>

Open in new window



I am not able to achieve that. Please help me with this issue.
chrisveeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
you would want to use substr where the initial position moves one increment at a time.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
You have a couple of variables too much

<?php
 function BreakSentence($text) {
 $two = array();
$length = mb_strlen($text);
 for($i=1;$i<$length;$i++)
{
  $char2 = ($text[$i].' '.$text[$i+1]);
   array_push($two, $char2);
}
 return $two;
} ?>
0
mankowitzCommented:
@guy - you put in an extra space and also need to stop the loop at one character before the end.

<?php
$txt = "php is good!";
for ($i=0; $i<strlen($txt)-1; $i++)
    $res[] = $txt[$i] . $txt[$i+1];

print_r($res);
?>

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Guy Hengel [angelIII / a3]Billing EngineerCommented:
Thanks. It's difficult to type in mobile...

<?php function BreakSentence($text)
 { $two = array(); 
$length = mb_strlen($text); 
for($i=0;$i<$length-1;$i++) {  
$char2 = ($text[$i].$text[$i+1]);   
array_push($two, $char2); 

return $two; 
} ?>
0
mankowitzCommented:
@guy -- I was trying to get that badge for answering questions by mobile, but i gave up. Code is way to hard to type with only thumbs!
0
Ray PaseurCommented:
As with most things in computer programming there are many ways to address the problem.  This worked for me.  Not sure what I would do if the data were UTF-8, but there are some PHP functions that work with that.  This article talks a bit about different character encoding.
Please see: http://iconoun.com/demo/temp_chrisvee.php

<?php // demo/temp_chrisvee.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28499438.html
// REF: http://www.php.net/manual/en/function.str-split.php

$str = "php is good";

// MAKE AN ARRAY OF THE LETTERS, AND PAD THE ARRAY WITH AN END-SIGNAL
$arr = str_split($str);
$arr[] = FALSE;

// ACCUMULATE THE LETTER PAIRS HERE
$out = array();

// WITH EACH LETTER
foreach ($arr as $key => $chr)
{
    // IF THE FOLLOWING LETTER IS THE END-SIGNAL WE ARE DONE
    if (!$arr[$key+1]) break;

    // OTHERWISE GATHER THIS PAIR OF LETTERS
    $out[] = $chr . $arr[$key+1];
}

// SHOW THE WORK PRODUCT
echo '<pre>';
var_dump($str);
print_r($out);

Open in new window

Best to all, ~Ray
0
Ray PaseurCommented:
trying to get that badge for answering questions by mobile
No kidding!
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Being in holiday without laptop, no choice...
0
Ray PaseurCommented:
Apologies... PHP is a loosely typed language and I neglected to think about "php is 0 good."  This example corrects that oversight.  See line 20.

<?php // demo/temp_chrisvee.php
error_reporting(E_ALL);

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28499438.html
// REF: http://www.php.net/manual/en/function.str-split.php

$str = "php is good";

// MAKE AN ARRAY OF THE LETTERS, AND PAD THE ARRAY WITH AN END-SIGNAL
$arr = str_split($str);
$arr[] = FALSE;

// ACCUMULATE THE LETTER PAIRS HERE
$out = array();

// WITH EACH LETTER
foreach ($arr as $key => $chr)
{
    // IF THE FOLLOWING LETTER IS THE END-SIGNAL WE ARE DONE
    if ($arr[$key+1] === FALSE) break;

    // OTHERWISE GATHER THIS PAIR OF LETTERS
    $out[] = $chr . $arr[$key+1];
}

// SHOW THE WORK PRODUCT
echo '<pre>';
var_dump($str);
print_r($out);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.