用SMARTY截断HTML格式的文本 [英] Truncate a HTML formatted text with SMARTY

查看:159
本文介绍了用SMARTY截断HTML格式的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用随机HTML代码格式化的变量。我将它称为 {$ text} ,并截断它。



该值为例如:

 < div> Lorem< i> ipsum< b> dolor< span>坐< / span> amet< / b> ;,con< / i> ELIT< / DIV> 

如果我截断文本的第一个〜30个字母,我会得到这个:

 < div> Lorem< i> ipsum< b> dolor< span> sit 
<

问题是,我无法关闭这些元素。因此,我需要一个脚本来检查代码中的<> 元素(其中*可以是任何内容),如果它没有关闭标记,则关闭'em。



请帮助我。感谢。



解决方案经过数小时后,4次投票结束@ stackoverflow: > PHP:

  ... 
函数closetags($ content){
preg_match_all('#< ;(?! meta | img | br | hr | input \ b)\ b([az] +)(?:。*)?(?<![/ | /])> #iU', $ content,$ result);
$ openedtags = $ result [1];
preg_match_all('#< /([a-z] +)> #iU',$ content,$ result);
$ closedtags = $ result [1];
$ len_opened = count($ openedtags);
if(count($ closedtags)== $ len_opened){
return $ content;
}
$ openedtags = array_reverse($ openedtags);
for($ i = 0; $ i <$ len_opened; $ i ++){
if(!in_array($ openedtags [$ i],$ closedtags)){
$ content。 ='< /'.$ openedtags [$ i]。'>';
} else {
unset($ closedtags [array_search($ openedtags [$ i],$ closedtags)]);
}
}
return $ content;

...

TPL:

  {$ pages [j] .text | truncate:300 | @closetags} 


解决方案

拉出所有打开的标签,将它们逐个推入数组(array_1)。



拉出所有已关闭的标签,将它们逐个推入一个数组(array_2)(这包括自闭标签)。



对于第一个数组(array_1)中未在第二个数组(array_2)中找到的标记,将它们添加到html中。



<当然,如果你不写出正确的html,这个方法就会失败......但是whatchagonnado?

>

另一种方法是在字符串中查看哪些标签已关闭并根据需要关闭。


I've got a variable which is formatted with random HTML code. I call it to {$text} and i truncate it.

The value is for example:

<div>Lorem <i>ipsum <b>dolor <span>sit </span>amet</b>, con</i> elit.</div>

If i truncate the text's first ~30 letters, I'll get this:

<div>Lorem <i>ipsum <b>dolor <span>sit 

The problem is, I can't close the elements. So, I need a script, which check the <*> elements in the code (where * could be anything), and if it dont have a close tag, close 'em.

Please help me in this. Thanks.

Solution after hours, and 4 vote-up @ stackoverflow:

PHP:

...
function closetags($content) {
   preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $content, $result);
    $openedtags = $result[1];
  preg_match_all('#</([a-z]+)>#iU', $content, $result);
 $closedtags = $result[1];
  $len_opened = count($openedtags);
  if (count($closedtags) == $len_opened) {
       return $content;
  }
  $openedtags = array_reverse($openedtags);
  for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)) {
         $content .= '</'.$openedtags[$i].'>';
       } else {
           unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
  }
  return $content;
}
...

the TPL:

{$pages[j].text|truncate:300|@closetags}

解决方案

Pull out all the open tags, push them into an array (array_1) one-by-one.

Pull out all of the closed tags, push them into an array (array_2) one-by-on (this includes self closing tags).

For the tags in the first array (array_1) that are not found in the second array (array_2), add them to the html.

[edit]

Of course, this method fails miserably if you do not write proper html... but whatchagonnado?

Another way would be to look ahead in the string to see which tags are closed and close them as needed.

这篇关于用SMARTY截断HTML格式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆