


下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
html包括⽂本内容,截断包含HTML的⽂本,忽略标签我已经按照您的建议编写了⼀个将HTML截断的函数,但是没有打印出来,⽽是将其保存在字符串变量中。也处理HTML实体。/***functiontotruncateandthencleanupendoftheHTML,*truncatesbycountingcharactersoutsideofHTMLtags**@authoralexlockwood,alexdotlockwoodatwebsightdesign**@paramstring$strthestringtotruncate*@paramint$lenthenumberofcharacters*@paramstring$endtheendstringfortruncation*@returnstring$truncated_html****/publicstaticfunctiontruncateHTML($str,$len,$end='…'){//findalltags$tagPattern='/(]*)>?|&[\w#]+;/i';//matchhtmltagsandentitiespreg_match_all($tagPattern,$str,$matches,PREG_OFFSET_CAPTURE|PREG_SET_ORDER);//WSDDebug::dump($matches);exit;$i=0;//loopthrougheachfoundtagthatiswithinthe$len,addthosecharacterstothelen,//alsotrackopenandclosedtags//$matches[$i][0]=thewholetagstring--theonlyapplicablefieldforhtmlenitities//IFitsnotmatchingan&htmlentity;thefollowingapply//$matches[$i][1]=thestartofthetageither'//$matches[$i][2]=thetagname//$matches[$i][3]=theendofthetag//$matces[$i][$j][0]=thestring//$matces[$i][$j][1]=thestroffestwhile($matches[$i][0][1]<$len&&!empty($matches[$i])){$len=$len+strlen($matches[$i][0][0]);if(substr($matches[$i][0][0],0,1)=='&')$len=$len-1;//if$matches[$i][2]isundefinedthenitsanhtmlentity,wanttoignorethosefortagcounting//ignoreempty/singletontagsfortagcountingif(!empty($matches[$i][2][0])&&!in_array($matches[$i][2][0],array('br','img','hr','input','param','link'))){//doublecheckif(substr($matches[$i][3][0],-1)!='/'&&substr($matches[$i][1][0],-1)!='/')$openTags[]=$matches[$i][2][0];elseif(end($openTags)==$matches[$i][2][0]){array_pop($openTags);}else{$warnings[]="htmlhassometagsmismatchedinit:$str";}}$i++;}$closeTags='';if(!empty($openTags)){$openTags=array_reverse($openTags);foreach($openTagsas$t){$closeTagString.="".$t.">";}}if(strlen($str)>$len){//Findsthelastspacefromthestringnewlength$lastWord=strpos($str,'',$len);if($lastWord){//truncatewithnewlenlastword$str=substr($str,0,$lastWord);//findslastcharacter$last_character=(substr($str,-1,1));//addtheendtext$truncated_html=($last_character=='.'?$str:($last_character==','?substr($str,0,-1):$str)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025供暖工程合同范本
- 2025年电工(高级)考试试卷及答案
- 2025股票发行辅导合同
- 肾下垂的临床护理
- 肠系膜脂膜炎的临床护理
- 新质生产力杉杉股份
- 2025技术实施许可合同书
- 2025年天津市房屋租赁合同模板
- 2025地下车库租赁合同范本
- 2025家居智能小家电区域代理商销售合同书
- 药品召回管理课件
- 石化工程质量管理培训
- 审计访谈系列之访谈提纲2021年
- 律师案件评估报告范文
- 《中国血糖监测临床应用指南(2021年版)》解读课件
- 【MOOC】构造地质学-中国地质大学(武汉) 中国大学慕课MOOC答案
- 【MOOC】模拟电子电路与技术基础-西安电子科技大学 中国大学慕课MOOC答案
- 医疗质量控制培训方案
- 病理性近视怎治疗
- 《工业机器人系统维护》试卷6及答案
- 设备调试人员培训
评论
0/150
提交评论