PHP 将文本拆分为140个char数组,用于twitter

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 将文本拆分为140个char数组,用于twitter相关的知识,希望对你有一定的参考价值。

/* 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;
}

以上是关于PHP 将文本拆分为140个char数组,用于twitter的主要内容,如果未能解决你的问题,请参考以下文章