• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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.
0
chrisvee
Asked:
chrisvee
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now