Shorten text without splitting words or breaking html tags

前端 未结 8 1357
粉色の甜心
粉色の甜心 2020-12-28 17:06

I am trying to cut off text after 236 chars without cutting words in half and preserving html tags. This is what I am using right now:

$shortdesc = $_helper-         


        
8条回答
  •  借酒劲吻你
    2020-12-28 17:52

    I did in JS, hope this logic will help in PHP too..

    splitText : function(content, count){
            var originalContent = content;
             content = content.substring(0, count);
              //If there is no occurance of matches before breaking point and the hit breakes in between html tags.
             if (content.lastIndexOf("<") > content.lastIndexOf(">")){
                content = content.substring(0, content.lastIndexOf('<'));
                count = content.length;
                if(originalContent.indexOf("', originalContent.indexOf("', count)+1);
                }
              //If the breaking point is in between tags.
             }else if(content.lastIndexOf("<") != content.lastIndexOf("', count)+1);
             }
            return content;
        },
    

    Hope this logic helps some one..

提交回复
热议问题