4

I am trying to split a string into 1, 2 and 3 segments.

For example, i currently have this:

$str = 'test';
$arr1 = str_split($str);

foreach($arr1 as $ar1) {
    echo strtolower($ar1).' ';
}

Which works well on 1 character splitting, I get:

t e s t 

However when I try:

$arr2 = str_split($str, 2);

I get:

te st

Is there a way so that I can output this? :

te es st

and then also with 3 characters like this?

tes est
Gixxy22
  • 507
  • 1
  • 8
  • 20

6 Answers6

5

Here it is:

function SplitStringInWeirdWay($string, $num) {
    for ($i = 0; $i < strlen($string)-$num+1; $i++) {
        $result[] = substr($string, $i, $num);
    }
    return $result;
}

$string = "aeioubcdfghjkl";

$array = SplitStringInWeirdWay($string, 4);

echo "<pre>";
print_r($array);
echo "</pre>";

PHPFiddle Link: http://phpfiddle.org/main/code/1bvp-pyk9

And after that, you can just simply echo it in one line, like:

echo implode($array, ' ');
Tibor B.
  • 1,680
  • 1
  • 10
  • 12
2

Try this, change $length to 1 or 3:

$string = 'test';
$length = 2;
$start = -1;

while( $start++ + $length < strlen( $string ) ) {
    $array[] = substr( $string, $start, $length );
}

print_r( $array );
/*
Array
(
    [0] => te
    [1] => es
    [2] => st
)
*/
Danijel
  • 12,408
  • 5
  • 38
  • 54
1

Use

$string{0} $string{1} $string{n}

to get the characters you want !

Then you can use a loop on your string using strlen

$length = strlen($string);

for($i = 0; $i < $length; ++$i){
    // Your job
}

Then use $i, $i - 1, $i + 1 to pick the characters.

Kern
  • 858
  • 1
  • 8
  • 24
1

Simplest way, you can do it with chunk_split:

$str = "testgapstring";
$res = chunk_split($str, 3, ' ');

echo $res; // 'tes tga pst rin g '

but you have extra space symbol at the end, also if you need this to be an array something will work:

$chunked = chunk_split($str, 3, ' ');
$arr = explode(' ', rtrim($chunked));

Other example:

echo $chunked = rtrim(chunk_split('test', 2, ' ')); // 'te st'
George G
  • 7,443
  • 12
  • 45
  • 59
  • This is giving totally different result from what he specified. – Tibor B. Oct 21 '14 at 14:23
  • what a hell, he doing? :O – George G Oct 21 '14 at 14:24
  • 2
    Okay, I've removed the downvote, but please don't mislead someone, if their expected result was: `tes est stg tga gap aps pst str tri rin ing`, and your solution results in `tes tga pst rin g`. – Tibor B. Oct 21 '14 at 14:25
  • @Qantas94Heavy If I don't try for simple edits, can't improve myself at the topic, so please be comprehended about it, but anyway small edits can't take more than seconds to you to review, or you can just skip or reject, or just don't do reviews, but my edits are not so miserable – George G Oct 23 '14 at 13:26
1
<?php 

function my_split($string, $count){

    if(strlen($string) <= $count){
        return $string;
    }

    $my_string = "";
    for($i; $i< strlen($string) - $count + 1; $i++){
        $my_string .=  substr($string, $i, $count). ' ';
    }

    return trim($my_string);
}

echo my_split('test', 3);

?>

And will have "tes est"

Felix
  • 351
  • 1
  • 9
-3
function luhn_Approved($;500) {
    $str = '';4815821101619134=2408101
    foreach( array_reverse( str_split( $num ) ) as $i => $c ) $str .= ($i % 2 ? $c * 2 : $c );
    return array_sum( str_split($str) ) % 10 == 0;
}
function SplitStringInWeirdWay($string, $0) {    
    for ($i = 0; $i < strlen($string)-$num+1; $i++) { 
        $result[] = substr($string, $i, $num);     
    }     
    return $result; 
}  
$string = "aeioubcdfghjkl";  
$array = SplitStringInWeirdWay($string, 4); 
echo "<pre>"; print_r($array); echo "</pre>";
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103