在php中html转换成文本提供了自带的函数strip_tags了,但有时此函数不够用,下面总结了一些用户自定的函数,各位可参考.
最常用的使用php函数strip_tags,代码如下:
- <?php
- $mystr=<<<SATO
- 此处省略几十行HTML代码^_^
- SATO;
- $str=strip_tags($mystr);
- //到这里就已经达到我的HTML转为TXT文本的目的了,哈哈,使用这个函数真方便
- //下面是插件的一些切词等操作,这里就不多说了
- ?>
自定义函数,代码如下:
- <?php
- // $document 应包含一个 HTML 文档。
- // 本例将去掉 HTML 标记,javascript 代码
- // 和空白字符。还会将一些通用的
- // HTML 实体转换成相应的文本。
- $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
- "'<[/!]*?[^<>]*?>'si", // 去掉 HTML 标记
- "'([rn])[s]+'", // 去掉空白字符
- "'&(quot|#34);'i", // 替换 HTML 实体
- "'&(amp|#38);'i",
- "'&(lt|#60);'i",
- "'&(gt|#62);'i",
- "'&(nbsp|#160);'i",
- "'&(iexcl|#161);'i",
- "'&(cent|#162);'i",
- "'&(pound|#163);'i",
- "'&(copy|#169);'i",
- "'&#(d+);'e"); // 作为 PHP 代码运行
- $replace = array ("",
- "",
- "1",
- """,
- "&",
- "<",
- ">",
- " ",
- chr(161),
- chr(162),
- chr(163),
- chr(169),
- "chr(1)");
- $text = preg_replace ($search, $replace, $document);
- ?>
后来我从网上看到了一个使用PHP写的方法,使用这个方法也可以实现将HTML转为TXT文本,个人觉得也还蛮实用的,在这里分享一下,代码如下:
- function HtmlToText($str){
- $str=preg_replace("/<sty(.*)/style>|<scr(.*)/script>|<!–(.*)–>/isU","",$str);//去除CSS样式、JS脚本、HTML注释
- $alltext="";//用于保存TXT文本的变量
- $start=1;//用于检测<左、>右标签的控制开关
- for($i=0;$i<strlen($str);$i++){//遍历经过处理后的字符串中的每一个字符
- if(($start==0)&&($str[$i]==">")){//如果检测到>右标签,则使用$start=1;开启截取功能
- $start=1;
- }else if($start==1){//截取功能
- if($str[$i]=="<"){//如果字符是<左标签,则使用<font color='red'>|</font>替换
- $start=0;
- $alltext.="<font color='red'>|</font>";
- }else if(ord($str[$i])>31){//如果字符是ASCII大于31的有效字符,则将字符添加到$alltext变量中
- $alltext.=$str[$i];
- }
- }
- }
- //下方是去除空格和一些特殊字符的操作
- $alltext = str_replace(" "," ",$alltext);
- $alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
- $alltext = preg_replace("/[ ]+/s"," ",$alltext);
- return $alltext;
- }
使用下面这个方法也可以实现将简答的HTML代码转换为TXT文本,实例代码如下:
- function html2text($str,$encode = 'GB2312')
- {
- $str = preg_replace("/<style .*?</style>/is", "", $str);
- $str = preg_replace("/<script .*?</script>/is", "", $str);
- $str = preg_replace("/<br s*/?/>/i", "n", $str);
- $str = preg_replace("/</?p>/i", "nn", $str);
- $str = preg_replace("/</?td>/i", "n", $str);
- $str = preg_replace("/</?div>/i", "n", $str);
- $str = preg_replace("/</?blockquote>/i", "n", $str);
- $str = preg_replace("/</?li>/i", "n", $str);
- $str = preg_replace("/ /i", " ", $str);
- $str = preg_replace("/ /i", " ", $str);
- $str = preg_replace("/&/i", "&", $str);
- $str = preg_replace("/&/i", "&", $str);
- $str = preg_replace("/</i", "<", $str);
- $str = preg_replace("/</i", "<", $str);
- $str = preg_replace("/“/i", '"', $str);
- $str = preg_replace("/&ldquo/i", '"', $str);
- $str = preg_replace("/‘/i", "'", $str);
- $str = preg_replace("/&lsquo/i", "'", $str);
- $str = preg_replace("/’/i", "'", $str);
- $str = preg_replace("/&rsquo/i", "'", $str);
- $str = preg_replace("/>/i", ">", $str);
- $str = preg_replace("/>/i", ">", $str);
- $str = preg_replace("/”/i", '"', $str);
- $str = preg_replace("/&rdquo/i", '"', $str);
- $str = strip_tags($str);
- $str = html_entity_decode($str, ENT_QUOTES, $encode);
- $str = preg_replace("/&#.*?;/i", "", $str);
- return $str;
- }
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » PHP将HTML转换成文本一些方法总结
波比源码 » PHP将HTML转换成文本一些方法总结
generic levaquin 250mg levaquin 500mg tablet
buy indocin pill cefixime 200mg ca order trimox 500mg online
buy isotretinoin 20mg sale azithromycin 250mg sale ivermectin cost uk
buy fosamax 70mg ibuprofen buy online purchase pepcid pills
olmesartan 10mg for sale oral olmesartan order diamox 250mg generic
prograf online order buy labetalol 100 mg ursodiol oral
assignment writing services online gambling sites online slot games
methotrexate for sale online warfarin 2mg brand buy metoclopramide 20mg generic
big fish casino online online blackjack game legal online blackjack
buy tamsulosin ondansetron pills spironolactone pill
order flagyl for sale metronidazole 200mg without prescription bactrim generic
fluconazole 100mg canada viagra 100mg price order viagra 50mg generic
order retin purchase tretinoin generic buy avanafil 200mg pill
buy naprosyn 250mg online cheap naprosyn 250mg cost order prevacid 30mg generic
biaxin 500mg without prescription buy antivert 25mg without prescription buy generic antivert 25mg
pioglitazone online order purchase viagra online cheap purchase sildenafil pills
nifedipine uk oral aceon buy allegra 120mg without prescription
real money poker online online roulette for real money casino online blackjack
order doxycycline generic buy doxycycline 200mg without prescription cleocin oral
temovate order buy temovate sale cordarone without prescription
amoxicillin price order stromectol without prescription can i buy ivermectin online
order dapoxetine 30mg for sale motilium oral oral motilium
order generic pepcid 40mg buy prograf pills remeron sale
order ropinirole 1mg calcitriol 0.25 mg over the counter order labetalol 100mg for sale
tadacip online buy buy trimox pills trimox canada
buy tricor 200mg online fenofibrate 200mg cheap sildenafil online order
Thank you great post. Hello Administ . Hacklink , Hacklink panel , Hacklink al Hacklink panel
order minocin 50mg pill neurontin 800mg usa hytrin canada
provigil tablet stromectol 3mg tablet order generic phenergan
clomiphene pills clomiphene 100mg drug buy prednisolone 40mg generic
Everything is very open and very clear explanation of issues. was truly information. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
sildenafil 50mg generic propecia for sale purchase propecia sale
generic ivermectin online ed medications purchase prednisone without prescription
prednisolone 5mg oral buy generic neurontin order furosemide 100mg generic
brand monodox purchase vibra-tabs online cheap oral acyclovir
order ditropan 5mg generic oxybutynin 2.5mg usa buy trileptal 300mg online cheap
purchase cefdinir omnicef 300mg uk cheap pantoprazole
cost alfuzosin 10 mg buy uroxatral 10 mg buy diltiazem pill
buy ezetimibe 10mg ezetimibe 10mg tablet buy methotrexate pill
order promethazine sale purchase provigil online tadalafil 10mg cheap
purchase warfarin without prescription buy coumadin 5mg generic generic allopurinol 300mg
buy levofloxacin without prescription buy bupropion 150 mg online cheap order generic zyban
buy zyrtec online buy strattera 25mg generic zoloft 50mg us
order femara 2.5 mg without prescription viagra 100mg pills for men viagra professional
oral tadalafil 10mg tadalafil 40mg uk ed pills online
stromectol 0.1 deltasone 5mg oral isotretinoin buy online
provigil 200mg sale buy phenergan without a prescription buy prednisone 5mg generic
buy generic amoxil zithromax 500mg cheap purchase prednisolone online cheap
order isotretinoin 10mg generic oral zithromax 250mg zithromax buy online
order generic gabapentin 100mg order doxycycline 100mg without prescription purchase doxycycline for sale
order prednisolone generic buy prednisolone 20mg online cheap lasix 40mg tablet
buy atenolol without prescription buy methylprednisolone paypal buy generic letrozole over the counter
metformin 500mg uk order generic metformin norvasc 5mg pills
biltricide generic order generic biltricide buy cyproheptadine
zestril ca lopressor 50mg brand buy metoprolol 50mg without prescription
generic methotrexate purchase methotrexate pill metoclopramide tablet
orlistat 120mg drug purchase xenical buy generic zyloprim
cheap cozaar buy hyzaar online buy topamax 100mg generic
buy imitrex medication buy cheap sumatriptan generic avodart
toradol 10mg canada order inderal 20mg generic propranolol for sale
order clopidogrel 75mg without prescription ketoconazole 200mg canada buy ketoconazole pills
order betnovate 20 gm sale how to buy itraconazole itraconazole buy online
where to buy combivent without a prescription ipratropium cheap order linezolid pills
buy starlix 120 mg online cheap oral atacand 8mg buy candesartan 16mg
purchase nebivolol bystolic over the counter buy clozapine medication
purchase zocor pill female viagra sildenafil buy sildenafil pills
purchase tegretol generic lincocin tablet lincomycin price
buy tadalafil 40mg cialis overnight shipping usa cheap sildenafil pill
purchase estradiol generic lamictal 50mg usa buy prazosin pill
purchase vermox tadalafil 20mg brand purchase tadalafil generic
order indomethacin 50mg for sale buy indomethacin 50mg order cefixime 100mg generic
buy bimatoprost generic order careprost pills buy trazodone pills
order clonidine 0.1mg generic where can i buy antivert buy tiotropium bromide 9 mcg sale
buy arava 20mg for sale genuine viagra order azulfidine
order tadalafil 40mg cheap viagra 50mg cheap cialis pill
lasix cheap ventolin inhalator cheap order ventolin inhalator online
order clobetasol generic order buspirone 10mg sale amiodarone 100mg drug
order temovate without prescription buspar for sale cordarone 100mg over the counter
order digoxin 250mg online purchase molnupiravir online where to buy molnupiravir without a prescription