0

以下のコードを使用して、長いメッセージを複数の短いメッセージに分割しています。最後のメッセージが最初に送信されるように、送信順序を逆にする最も簡単な方法は何ですか?

(メッセージは3>2>1、現在の順序ではなく、この順序で送信する必要があります1>2>3)

function split_to_chunks($to,$text){
    $total_length = (137 - 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 . ' ';
            }
        }
    }


$length = count($message);

for ($i = 0; $i < $length; $i++) {
    if($i < $length-1){
$status = new Tweet();
$status->set($message[$i]."...");
  } else {
$status = new Tweet();
$status->set($message[$i]);
}
  }
}
4

2 に答える 2

5

ループarray_reverseの直前に使用foreach

この関数を使用すると、ループを実行する前に配列array_reverseを逆にすることができます(なぜ? ではなく、とにかくすべてを調べているように見えます)。$messageforforeach

このコードの単純なリファクタリング

まず第一に、コードは 2 つの関数である必要があります。これは、まったく関連のない 2 つのタスクを実行しているためです。両方を呼び出すラッパー関数が必要な場合は、必ずそうしてください。

入力テキストをツイートに分割する関数は次のとおりです。

// This function will separate an arbitrary length input text into 137 or less chatracter tweets.
function separateTextIntoTweets($input, $reversed = true) {

    // Remove line breaks from input, then allocate to an array
    $input = trim(preg_replace('/\s\s+/', ' ', $input));
    $text_arr = explode(" ", $input);

    $tweets  = array();
    $tweet = "";

    // Using a while loop, we can check the length of $text_arr on each iteration
    while(!empty($text_arr)) {
        // Take first word out of the array
        $word = array_shift($text_arr);
        if(strlen($tweet) + strlen($word) < 137) { // Fits in this tweet
            $tweet .= " $word";
        } else { // Does not fit in this tweet
            $tweets[] = trim($tweet);
            $tweet    = $word;
        }
    }

    // If there's a leftover tweet, add it to the array
    if(!empty($tweet)) $tweets[] = $tweet;

    // Return tweets in the reversed order unless $reversed is false
    return ($reversed) ? array_reverse($tweets) : $tweets;
}

これのライブデモンストレーション

そして、これはマルチパートツイートを送信する関数で、配列内の最後のツイートを除くすべてに「...」を追加します:

// This function sends tweets, appending '...' to continue
function sendTweets($tweets) {
    foreach($tweets as &$tweet) {
        $status = new Tweet();
        $tweet = ($tweet == end($tweets)) ? $tweet : $tweet . "...";
        $status->set($tweet);
    }
}

sendTweets目的の結果を得るために の出力を直接呼び出すことができるように、これを設計しましseparateTextIntoTweetsた。

あまり標準的でない機能の説明

必要に応じて、コードのあまり明白でない部分の説明:

&$tweet                             
    - Passes $tweet by reference so that it can be modified to append '...'
$tweet = ($tweet == end($tweets)) ? $tweet : $tweet . "..."                      
    - Conditional ternary operator, this is shorthand for:
        if($tweet == end($message)) {
            $tweet = $tweet;
        } else {
            $tweet = $tweet . "...";
        }
end($tweets)                      
    - Refers to the last element in the $tweets array
array_shift                         
    - Removes and returns the first element from an array
strlen                              
    - Length of a string
preg_replace('/\s\s+/', ' ', $input) 
    - Replaces excess whitespace, and newlines, with a single space.
于 2013-09-06T10:56:53.310 に答える
1

array_reverse関数$messagesを使用して、最後のループの直前に配列を逆にすることができます。

$messages = array_reverse($messages);
于 2013-09-06T10:56:27.983 に答える