3

テキストのチャンクを Google に送信して翻訳するスクリプトを作成しましたが、テキスト (html ソース コード) が html タグの途中で分割されてしまい、Google が間違ったコードを返すことがあります。

文字列を配列に分割する方法は既に知っていますが、出力文字列が 5000 文字を超えず、タグで分割されないようにしながら、これを行うより良い方法はありますか?

更新:回答のおかげで、これは私がプロジェクトで使用することになったコードであり、うまく機能します

function handleTextHtmlSplit($text, $maxSize) {
    //our collection array
    $niceHtml[] = '';

    // Splits on tags, but also includes each tag as an item in the result
    $pieces = preg_split('/(<[^>]*>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

    //the current position of the index
    $currentPiece = 0;

    //start assembling a group until it gets to max size

    foreach ($pieces as $piece) {
        //make sure string length of this piece will not exceed max size when inserted
        if (strlen($niceHtml[$currentPiece] . $piece) > $maxSize) {
            //advance current piece
            //will put overflow into next group
            $currentPiece += 1;
            //create empty string as value for next piece in the index
            $niceHtml[$currentPiece] = '';
        }
        //insert piece into our master array
        $niceHtml[$currentPiece] .= $piece;
    }

    //return array of nicely handled html
    return $niceHtml;
}
4

3 に答える 3

3

注: これをテストする機会はありませんでした (そのため、1 つまたは 2 つのマイナーなバグがある可能性があります) が、アイデアが得られるはずです:

function get_groups_of_5000_or_less($input_string) {

    // Splits on tags, but also includes each tag as an item in the result
    $pieces = preg_split('/(<[^>]*>)/', $input_string,
        -1, PREG_SPLIT_DELIM_CAPTURE);

    $groups[] = '';
    $current_group = 0;

    while ($cur_piece = array_shift($pieces)) {
        $piecelen = strlen($cur_piece);

        if(strlen($groups[$current_group]) + $piecelen > 5000) {
            // Adding the next piece whole would go over the limit,
            // figure out what to do.
            if($cur_piece[0] == '<') {
                // Tag goes over the limit, just put it into a new group
                $groups[++$current_group] = $cur_piece;
            } else {
                // Non-tag goes over the limit, split it and put the
                // remainder back on the list of un-grabbed pieces
                $grab_amount = 5000 - $strlen($groups[$current_group];
                $groups[$current_group] .= substr($cur_piece, 0, $grab_amount);
                $groups[++$current_group] = '';
                array_unshift($pieces, substr($cur_piece, $grab_amount));
            }
        } else {
            // Adding this piece doesn't go over the limit, so just add it
            $groups[$current_group] .= $cur_piece;
        }
    }
    return $groups;
}

また、これは通常の単語の途中で分割される可能性があることにも注意してください。それが望ましくない場合は、 で始まる部分を変更して// Non-tag goes over the limit、 のより適切な値を選択して$grab_amountください。これはドロップイン ソリューションではなく、タグの分割を回避する方法の例にすぎないため、わざわざコーディングしませんでした。

于 2010-07-20T21:53:04.517 に答える
0

Googleに送信する前に、文字列からhtmlタグを削除してみませんか。PHPには、これを実行できるstrip_tags()関数があります。

于 2010-07-20T21:30:03.383 に答える
0

preg_split良い正規表現であなたのためにそれをするでしょう。

于 2010-07-20T21:31:28.937 に答える