Return to Snippet

Revision: 25900
at April 13, 2010 14:45 by s0l1dsnak3123


Updated Code
/* split_to_chunks by John Hamelink 2010. This code is in the PUBLIC DOMAIN. */

function split_to_chunks($to,$text){
	$total_length = (140 - strlen($to));
	$text_arr = explode(" ",$text);
	$i=0;
	$message[0]="";
	foreach ($text_arr as $word){
		if ( strlen($message[$i] . $word . ' ') <= $total_length ){
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] .= $word;
			} else {
				$message[$i] .= $word . ' ';
			}
		} else {
			$i++;
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] = $word;
			} else {
				$message[$i] = $word . ' ';
			}
		}
	}
	return $message;
}

Revision: 25899
at April 13, 2010 14:41 by s0l1dsnak3123


Initial Code
function split_to_chunks($to,$text){
	$total_length = (140 - strlen($to));
	$text_arr = explode(" ",$text);
	$i=0;
	$message[0]="";
	foreach ($text_arr as $word){
		if ( strlen($message[$i] . $word . ' ') <= $total_length ){
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] .= $word;
			} else {
				$message[$i] .= $word . ' ';
			}
		} else {
			$i++;
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] = $word;
			} else {
				$message[$i] = $word . ' ';
			}
		}
	}
	return $message;
}

Initial URL

                                

Initial Description
split_to_chunks splits the string up into an array with elements containing 140 characters <b>or less</b>. This enables you to keep words intact when sending automated twitter messages.

$to is needed in order to subtract from 140 the amount of characters allowed. This is important because a long username could create problems with messages being too long. This could be done pragmatically, and would be relatively simple to do, but was not needed in my case and so was not implemented.

$text contains a string containing any amount of characters - including under 140.

$message is an array which gives the message <b>minus</b> the username at the front.

Initial Title
Split text up into 140 char array for twitter

Initial Tags
array, text, twitter

Initial Language
PHP