Php 在不破坏html标记的情况下剪切文本

Php 在不破坏html标记的情况下剪切文本,php,html,parsing,string,Php,Html,Parsing,String,有没有一种方法可以在不编写自己的函数的情况下实现这一点 例如: $text = 'Test <span><a>something</a> something else</span>.'; $text = cutText($text, 2, null, 20, true); //result: Test <span><a>something</a></span> $text=”测试 但我需要一个更好的

有没有一种方法可以在不编写自己的函数的情况下实现这一点

例如:

$text = 'Test <span><a>something</a> something else</span>.';
$text = cutText($text, 2, null, 20, true);
//result: Test <span><a>something</a></span>
$text=”测试
但我需要一个更好的解决方案。我想保持嵌套标记不变

到目前为止,我的算法是:

function cutText($content, $max_words, $max_chars, $max_word_len, $html = false) {
    $len = strlen($content);
    $res = '';

    $word_count = 0;
    $word_started = false;
    $current_word = '';
    $current_word_len = 0;

    if ($max_chars == null) {
        $max_chars = $len;
    }
    $inHtml = false;
    $openedTags = array();
    for ($i = 0; $i<$max_chars;$i++) {

        if ($content[$i] == '<' && $html) {
            $inHtml = true;
        }

        if ($inHtml) {
            $max_chars++;
        }       

        if ($html && !$inHtml) {

            if ($content[$i] != ' ' && !$word_started) {
                $word_started = true;
                $word_count++;
            }

            $current_word .= $content[$i];
            $current_word_len++;

            if ($current_word_len == $max_word_len) {
                $current_word .= '- ';
            }

            if (($content[$i] == ' ') && $word_started) {
                $word_started = false;
                $res .= $current_word;
                $current_word = '';
                $current_word_len = 0;
                if ($word_count == $max_words) {
                    return $res;
                }
            }
        }

        if ($content[$i] == '<' && $html) {
            $inHtml = true;
        }
    }
    return $res;
}
function cutText($content、$max\u words、$max\u chars、$max\u word\u len、$html=false){
$len=strlen($content);
$res='';
$word\u count=0;
$word_start=false;
$current_word='';
$current\u word\u len=0;
如果($max_chars==null){
$max_chars=$len;
}
$inHtml=false;
$openedTags=array();

对于($i=0;$i请尝试以下内容

  function cutText($inputText, $start, $length) {
    $temp = $inputText;
    $res = array();
    while (strpos($temp, '>')) {
      $ts = strpos($temp, '<');
      $te = strpos($temp, '>');
      if ($ts > 0) $res[] = substr($temp, 0, $ts);
      $res[] = substr($temp, $ts, $te - $ts + 1);
      $temp = substr($temp, $te + 1, strlen($temp) - $te);
      }
    if ($temp != '') $res[] = $temp;
    $pointer = 0; 
    $end = $start + $length - 1;
    foreach ($res as &$part) {
      if (substr($part, 0, 1) != '<') {
        $l = strlen($part);
        $p1 = $pointer;
        $p2 = $pointer + $l - 1;
        $partx = "";
        if ($start <= $p1 && $end >= $p2) $partx = "";
        else {
          if ($start > $p1 && $start <= $p2) $partx .= substr($part, 0, $start-$pointer);
          if ($end >= $p1 && $end < $p2) $partx .= substr($part, $end-$pointer+1, $l-$end+$pointer);
          if ($partx == "") $partx = $part;
          }
        $part = $partx;
        $pointer += $l;
        }
      }
    return join('', $res);
    }
输出(删除“测试部分”)

输出

string(48) "Tt <span><a>something</a> something else</span>."
string(32) "Test <span><a>some</a>se</span>."
输出

string(48) "Tt <span><a>something</a> something else</span>."
string(32) "Test <span><a>some</a>se</span>."
string(32)“测试一些东西。”

希望这有帮助


嗯,也许这不是最好的解决办法,但这是我目前能做的一切。

好的,我解决了这个问题

我把它分为两部分。 首先在不破坏html的情况下剪切文本:

function cutHtml($content, $max_words, $max_chars, $max_word_len) {
    $len = strlen($content);
    $res = '';

    $word_count = 0;
    $word_started = false;
    $current_word = '';
    $current_word_len = 0;

    if ($max_chars == null) {
        $max_chars = $len;
    }
    $inHtml = false;
    $openedTags = array();
    $i = 0;

    while ($i < $max_chars) {

        //skip any html tags
        if ($content[$i] == '<') {
            $inHtml = true;
            while (true) {
                $res .= $content[$i];
                $i++;
                while($content[$i] == ' ') { $res .= $content[$i]; $i++; }

                //skip any values
                if ($content[$i] == "'") {
                    $res .= $content[$i];
                    $i++;
                    while(!($content[$i] == "'" && $content[$i-1] != "\\")) {
                        $res .= $content[$i];
                        $i++;
                    }                   
                }

                //skip any values
                if ($content[$i] == '"') {
                    $res .= $content[$i];
                    $i++;
                    while(!($content[$i] == '"' && $content[$i-1] != "\\")) {
                        $res .= $content[$i];
                        $i++;
                    }                   
                }
                if ($content[$i] == '>') { $res .= $content[$i]; $i++; break;}
            }
            $inHtml = false;
        }

        if (!$inHtml) {

            while($content[$i] == ' ') { $res .= $content[$i]; $letter_count++; $i++; } //skip spaces

            $word_started = false;
            $current_word = '';
            $current_word_len = 0;
            while (!in_array($content[$i], array(' ', '<', '.', ','))) {

                if (!$word_started) {
                    $word_started = true;
                    $word_count++;
                }

                $current_word .= $content[$i];
                $current_word_len++;

                if ($current_word_len == $max_word_len) {
                    $current_word .= '-';
                    $current_word_len = 0;
                }

                $i++;
            }

            if ($letter_count > $max_chars) {
                return $res;
            }

            if ($word_count < $max_words) {
                $res .= $current_word;
                $letter_count += strlen($current_word);
            }

            if ($word_count == $max_words) {
                $res .= $current_word;
                $letter_count += strlen($current_word);
                return $res;
            }
        }

    }
    return $res;
}
函数cutHtml($content、$max\u words、$max\u chars、$max\u word\u len){
$len=strlen($content);
$res='';
$word\u count=0;
$word_start=false;
$current_word='';
$current\u word\u len=0;
如果($max_chars==null){
$max_chars=$len;
}
$inHtml=false;
$openedTags=array();
$i=0;
而($i<$max_chars){
//跳过任何html标记
如果($content[$i]=''){$res.=$content[$i];$i++;break;}
}
$inHtml=false;
}
如果(!$inHtml){
而($content[$i]=''){$res.=$content[$i];$letter_count++;$i++;}//跳过空格
$word_start=false;
$current_word='';
$current\u word\u len=0;
而(!in_array($content[$i],array('',){//开始读取第一个实际字符串
$reading=true;
$html[$i]=strtolower($html[$i]);//标记为小写
$closeTag.=$html[$i];
$i++;
}
$c=计数($openedTags);
如果($c>0&&$openedTags[$c-1]==$closeTag)数组_pop($openedTags);
}
if(!$reading)//只读标记
而($html[$i]!=''&$html[$i]!='>'){//开始读取第一个实际字符串
$reading=true;
$html[$i]=strtolower($html[$i]);//标记为小写
$tag.=$html[$i];
$i++;
}
//跳过任何值
如果($html[$i]==“'”){
$i++;
而(!($html[$i]==“'”&&&html[$i-1]!=“\\”){
$i++;
}                   
}
//跳过任何值
如果($html[$i]==''”){
$i++;
而(!($html[$i]=''“&&&html[$i-1]!='\\”){
$i++;
}                   
}
if($reading&&$html[$i]=='/'){//自关闭标记
$tag='';
打破
}
}
如果(!empty($tag))$openedTags[]=$tag;
}
}
而(计数($openedTags)>0){
$tag=array\u pop($openedTags);
$html.=”;
}
}
这不是白痴证明,但tinymce将清除这件事,所以进一步的清洁是没有必要的


它可能有点长,但我认为它不会消耗很多资源,而且应该比regex快。

这对我来说非常适合:

function trimContent ($str, $trimAtIndex) {

    $beginTags = array();       
    $endTags = array();

    for($i = 0; $i < strlen($str); $i++) {
        if( $str[$i] == '<' )
            $beginTags[] = $i;
        else if($str[$i] == '>')
           $endTags[] = $i;
    }

    foreach($beginTags as $k=>$index) {
        // Trying to trim in between tags. Trim after the last tag
        if( ( $trimAtIndex >= $index ) && ($trimAtIndex <= $endTags[$k])  ) {
            $trimAtIndex = $endTags[$k];
        }
    }

    return substr($str, 0, $trimAtIndex);
}
函数trimContent($str,$trimAtIndex){
$beginTags=array();
$endTags=array();
对于($i=0;$i$index开头){
//尝试在标记之间修剪。在最后一个标记之后修剪

如果($trimAtIndex>=$index)&($trimAtIndex@Kaminari-我已经对这个函数进行了两次测试,但仍然不能保证它在所有可能的情况下都能工作。问题是,我不想把单词一分为二。我需要在不破坏html的情况下,从整个内容中生成一个干净的简短内容
  $text = cutText($text, 9, 18);
string(32) "Test <span><a>some</a>se</span>."
function cutHtml($content, $max_words, $max_chars, $max_word_len) {
    $len = strlen($content);
    $res = '';

    $word_count = 0;
    $word_started = false;
    $current_word = '';
    $current_word_len = 0;

    if ($max_chars == null) {
        $max_chars = $len;
    }
    $inHtml = false;
    $openedTags = array();
    $i = 0;

    while ($i < $max_chars) {

        //skip any html tags
        if ($content[$i] == '<') {
            $inHtml = true;
            while (true) {
                $res .= $content[$i];
                $i++;
                while($content[$i] == ' ') { $res .= $content[$i]; $i++; }

                //skip any values
                if ($content[$i] == "'") {
                    $res .= $content[$i];
                    $i++;
                    while(!($content[$i] == "'" && $content[$i-1] != "\\")) {
                        $res .= $content[$i];
                        $i++;
                    }                   
                }

                //skip any values
                if ($content[$i] == '"') {
                    $res .= $content[$i];
                    $i++;
                    while(!($content[$i] == '"' && $content[$i-1] != "\\")) {
                        $res .= $content[$i];
                        $i++;
                    }                   
                }
                if ($content[$i] == '>') { $res .= $content[$i]; $i++; break;}
            }
            $inHtml = false;
        }

        if (!$inHtml) {

            while($content[$i] == ' ') { $res .= $content[$i]; $letter_count++; $i++; } //skip spaces

            $word_started = false;
            $current_word = '';
            $current_word_len = 0;
            while (!in_array($content[$i], array(' ', '<', '.', ','))) {

                if (!$word_started) {
                    $word_started = true;
                    $word_count++;
                }

                $current_word .= $content[$i];
                $current_word_len++;

                if ($current_word_len == $max_word_len) {
                    $current_word .= '-';
                    $current_word_len = 0;
                }

                $i++;
            }

            if ($letter_count > $max_chars) {
                return $res;
            }

            if ($word_count < $max_words) {
                $res .= $current_word;
                $letter_count += strlen($current_word);
            }

            if ($word_count == $max_words) {
                $res .= $current_word;
                $letter_count += strlen($current_word);
                return $res;
            }
        }

    }
    return $res;
}
function cleanTags(&$html) {
    $count = strlen($html);
    $i = -1;
    $openedTags = array();

    while(true) {
        $i++;
        if ($i >= $count) break;
        if ($html[$i] == '<') {

            $tag = '';
            $closeTag = '';
            $reading = false;
            //reading whole tag
            while($html[$i] != '>') {
                $i++;

                while($html[$i] == ' ') $i++; //skip any spaces (need to be idiot proof)
                if (!$reading && $html[$i] == '/') { //closing tag
                    $i++;
                    while($html[$i] == ' ') $i++; //skip any spaces

                    $closeTag = '';

                    while($html[$i] != ' ' && $html[$i] != '>') { //start reading first actuall string
                        $reading = true;
                        $html[$i] = strtolower($html[$i]); //tags to lowercase
                        $closeTag .= $html[$i];
                        $i++;
                    }
                    $c = count($openedTags);
                    if ($c > 0 && $openedTags[$c-1] == $closeTag) array_pop($openedTags);
                }

                if (!$reading) //read only tag
                while($html[$i] != ' ' && $html[$i] != '>') { //start reading first actuall string
                    $reading = true;
                    $html[$i] = strtolower($html[$i]); //tags to lowercase
                    $tag .= $html[$i];
                    $i++;
                }

                //skip any values
                if ($html[$i] == "'") {
                    $i++;
                    while(!($html[$i] == "'" && $html[$i-1] != "\\")) {
                        $i++;
                    }                   
                }

                //skip any values
                if ($html[$i] == '"') {
                    $i++;
                    while(!($html[$i] == '"' && $html[$i-1] != "\\")) {
                        $i++;
                    }                   
                }

                if ($reading && $html[$i] == '/') { //self closed tag
                    $tag = '';
                    break;
                }
            }
            if (!empty($tag)) $openedTags[] = $tag;

        }

    }

    while (count($openedTags) > 0) {
        $tag = array_pop($openedTags);
        $html .= "</$tag>";
    }
}
function trimContent ($str, $trimAtIndex) {

    $beginTags = array();       
    $endTags = array();

    for($i = 0; $i < strlen($str); $i++) {
        if( $str[$i] == '<' )
            $beginTags[] = $i;
        else if($str[$i] == '>')
           $endTags[] = $i;
    }

    foreach($beginTags as $k=>$index) {
        // Trying to trim in between tags. Trim after the last tag
        if( ( $trimAtIndex >= $index ) && ($trimAtIndex <= $endTags[$k])  ) {
            $trimAtIndex = $endTags[$k];
        }
    }

    return substr($str, 0, $trimAtIndex);
}