正則表達(dá)式去掉html標(biāo)簽的簡單介紹
str = #39ltspan style=\quotfontfamily\#39Arial\#39,\#39sansserif\#39fontsize9pt\quottestltspan#39$str=stripslashes$strstr = preg_replace#39#lt^\s+^*##39,#39lt$1#39,$strecho html。
為大家演示一個(gè)較為簡單的函數(shù)吧,這一個(gè)函數(shù)所要做的事情就是要將保留的TAG通通串起來,然后生成一個(gè)正則表達(dá)式,接著就要將一些并不需要的TAG通通刪除具體的函數(shù),如圖所示大家可以發(fā)現(xiàn)上面這一個(gè)正則表達(dá)式里面是有bug;String reg = quotlt?html^*quotString html = quotquothtmlreplaceAllreg,quotquot;1PHP替換原內(nèi)容$content = quotlttanlelttrlttd這是第一個(gè)td內(nèi)容lttdlttd這是第二個(gè)td內(nèi)容lttdlttrlttablequot匹配模式$preg = #39lt*?is#39所有的html標(biāo)簽都將被替換成空字符串$content =;使用正則表達(dá)式去掉html標(biāo)簽的方法常用的正則表達(dá)式是lt^lt+?g 1定義含有html標(biāo)簽的字符串text = #39ltpTest paragraphltplt! Comment lta href=quot#fragmentquotOther textlta#392定義正則表達(dá)式;易語言正則表達(dá)式 style=\quot*?\quot 替換成空字符串 quotquot我給你一個(gè)Javascript語言的例子,你看看吧ltscript type=textjavascript var s =quotltP style=\quotWHITESPACE normal WORDSPACING 0px TEXTTRANSFORM;把除了ltBR ltBR ltBR ltIMG ltIMG的ltxxxx都刪除i表示忽略大小寫JS replacelt?!\?BR\?IMG^lt*ig,#39#39PERL slt?!\?BR\?IMG^lt*ig 疏忽了,有些地方?jīng)]考慮全;用JavaScript方法如下,JAVA語言類似#39你的HTML文本#39replace++lt+,#39$1#39。
如果只要 ltb 標(biāo)簽,不用“過濾”的方法,用“提取”的方法更簡單str = #39ltimg src=quotxxxquotltbaaaltbltbr\nltbb\nbbltbltspan style=quotcolor#FF0000quotyyyltspan#39pattern = #39ltb?!lt\b;用正則表達(dá)式去掉html標(biāo)簽,下面是它的代碼,直接復(fù)制就可以用的代碼public static string StripHTMLstring HTML google quotStripHTMLquot 得到 string Regexs = quotltscript^*?*?ltscriptquot,quotlt;lt?php string=quotltphp1ltp02ltpltp888ltpltphpltp123ltpltphpltp234ltpltphpquotpattern = #39ltphp\s\S*lt\phpiU#39preg_match_all$pattern,$string,$dforeach $d1 as。
1過濾所有html標(biāo)簽的屬性的正則表達(dá)式search = array quot#39ltscript^*?*?ltscript#39siquot, 去掉 JavaScript quot#39lt!*?^lt*?#39siquot, 去掉 HTML 標(biāo)記 quot#39\r\n\s+#39quot;lt?azAZ+^lt* 這個(gè)表達(dá)式可以去掉所有HTML的標(biāo)簽 JAVA代碼可以這樣寫public static String delTagsFContentString content String patternTag = quotlt?azAZ+^lt*quotString patternBlank = quot^;正則表達(dá)式 lt\!img*?\? 我給你一個(gè)Javascript語言的例子,你看看吧 ltscript type=textjavascript var s =#39lthtml ltbody ltdivasdasdltdiv ltpavbcltimg src=quotxxquotllllltpltbodylt;String regEx_style=quotltstyle^*?sS*?ltstylequot 定義style的正則表達(dá)式 String regEx_html=quotlt^+quot 定義HTML標(biāo)簽的正則表達(dá)式 Pattern p_script=PatterncompileregEx_script。
import retest=#39ltp class=quotpictextquot align=quotcenterquot陳細(xì)妹ltp#39test=resubr#39lt^\s+\s^+?#39, r#39\1\2#39, testprinttest;html = quotlttable width=#39100%#39123lttableabcdadfadlttable123lttablequot$html = preg_replace#39lttable*width=\#39\quot100%\#39\quot*lt\tableiU#39,#39#39,$htmlecho $html要是你用瀏覽器調(diào)試。
掃描二維碼推送至手機(jī)訪問。
版權(quán)聲明:本文由飛速云SEO網(wǎng)絡(luò)優(yōu)化推廣發(fā)布,如需轉(zhuǎn)載請注明出處。