Unstoppable Domains

Alternating string splits

Spacemail by SpaceshipSpacemail by Spaceship
Watch

Outer

Established Member
Impact
2
I need to know how to break a string up into alternating sizes.

for example, I need this string broken up: "hi, how are you, peace!"
so results are like this:
"hi"
", h"
"ow"
" ar"
" y"
"ou "
", p"
"eac"
"e!"

Note the spaces. Basically, I need a string to be cut up by a pattern of 2 characters, then 3 characters, then 2 characters, then three characters

How would I do that in PHP?

Thank You :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Try this (I think you forgot the 'e' in 'are' in your example =P)

PHP:
	$string = 'hi, how are you, peace!';

	function customSplitString($text) {
		$text_pieces = array();
		$offset = 0;
		$piece_length = 2;
		$str_length = strlen($text);
		
		for ($x=0; $offset < $str_length; $x++) {
			$piece_length = ($x % 2) ? 3 : 2;
			$text_pieces[] = substr($text, $offset, $piece_length);
			$offset += $piece_length;
		}
		return $text_pieces;
	}

	print_r(customSplitString($string));
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back