Split a string after nth char


I’d like to split a string after its nth char (for exemple, after the 179nth char). I know how to split a string based on patterns, but not on char numbers.

Is it feasible?



Hi there,

Not sure if you want to get the rest of the string from the nth character or you want to split it evey nth character.

For the 1st, you could use substr():

$cut_var = substr($string, n-1);

Remember that if you’re using UTF-8, I’ll recommend using mb_substr(). It’s n-1 because it starts at 0.

If, on the other hand, you want the second possibility, you’ll probably need to use a function of your own (I don’t recall any function doing this on PHP, correct me if I’m wrong, please), my guess:

function cutString($string, $count) {
	$cut_arr = array();
	for ($i = 0; $i < ceil(strlen($string) / $count); $i++) {
		$cut_arr[] = substr($string, ($count * $i), $count);
	return $cut_arr;

I got up a few minutes ago, so there’s probably a better way to do it. :rolleyes:

Good luck!

Not sure exactly what you mean but take a look at: [fphp]chunk_split[/fphp] and [fphp]word_wrap[/fphp]