Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将大字符串拆分为数组,但拆分点不能打断标记 我编写了一个脚本,发送谷歌的文本块来翻译,但有时,HTML源代码的文本将在HTML标签中间分裂,谷歌会错误地返回代码。_Php_String Split - Fatal编程技术网

Php 将大字符串拆分为数组,但拆分点不能打断标记 我编写了一个脚本,发送谷歌的文本块来翻译,但有时,HTML源代码的文本将在HTML标签中间分裂,谷歌会错误地返回代码。

Php 将大字符串拆分为数组,但拆分点不能打断标记 我编写了一个脚本,发送谷歌的文本块来翻译,但有时,HTML源代码的文本将在HTML标签中间分裂,谷歌会错误地返回代码。,php,string-split,Php,String Split,我已经知道如何将字符串拆分为数组,但在确保输出字符串不超过5000个字符且不拆分到标记上的同时,是否有更好的方法来执行此操作 更新:感谢您的回答,这是我在项目中最终使用的代码,效果非常好 function handleTextHtmlSplit($text, $maxSize) { //our collection array $niceHtml[] = ''; // Splits on tags, but also includes each tag as an it

我已经知道如何将字符串拆分为数组,但在确保输出字符串不超过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;
}
函数handleTextHtmlSplit($text,$maxSize){
//我们的收集阵列
$niceHtml[]='';
//拆分标记,但也将每个标记作为一个项目包含在结果中
$pieces=preg_split('/(]*>)/',$text,-1,preg_split_DELIM_CAPTURE);
//索引的当前位置
$currentPiece=0;
//开始组装组,直到其达到最大大小
foreach($pieces作为$piece){
//插入时,确保该部件的字符串长度不会超过最大尺寸
如果(strlen($niceStml[$currentPiece].$piece)>$maxSize){
//前进电流片
//将溢出放入下一组
$currentPiece+=1;
//创建空字符串作为索引中下一项的值
$niceHtml[$currentPiece]='';
}
//将一块插入我们的主阵列
$niceHtml[$currentPiece]。=$piece;
}
//返回处理良好的html的数组
返回$html;
}

为什么不在将字符串发送到google之前从字符串中去掉html标记呢。PHP有一个函数可以为您实现这一点。

preg_split
使用一个好的正则表达式可以为您实现这一点。

注意:还没有机会测试这一点(因此可能有一两个小错误),但它应该能让您了解:

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;
}
函数获取小于等于5000的组($input\u string){
//拆分标记,但也将每个标记作为一个项目包含在结果中
$pieces=preg_split('/(]*>)/',$input_string,
-1、预分离(捕获);
$groups[]='';
$current_group=0;
而($cur\u piece=array\u shift($pieces)){
$piecelen=strlen($cur_-piece);
如果(strlen($groups[$current_group])+$piecelen>5000){
//加上下一个整体会超过极限,
//弄清楚该怎么做。

如果($cur_-piece[0]=='因为我需要保持html的完整性,因为它最终会呈现在页面上。Google translate不会去掉html本身吗?不,它忽略了html标记和属性,而不是“alt”。它会返回未触及的html标记和属性。谢谢你。它会让我的轮子旋转。我会尝试一下。