作者tnsshnews (tnsshnews)
看板PHP
標題[請益]要怎樣將特定的html tag所包夾的字串清掉
時間Thu Dec 30 11:10:13 2010
html source:
<p>These days, robots are more common than you might think. Consider the Roomba, a programmable robotic vacuum which cleans floors on its own.<br /><br />"We wanted to make something simple that people could use every day," says Helen Greiner, co-founder of iRobot, the company that makes the smart vacuum. "And that\'s what inspired us to build the Roomba." <div class=\'boxout photo230px\'><img src="
http://media.voanews.com/images/230*173/inline-US_ROBOT_CONVENTION_04.jpg" width="230" height="173"
alt="Some robots, like the Roomba - a programmable robotic vacuum which cleans floors on its own - are already commonplace in many US households. " title="Some robots, like the Roomba - a programmable robotic vacuum which cleans floors on its own - are already commonplace in many US households. " border="0" /><h6 class=\'credit\'>iRobot</h6><span class=\'caption\'>Some robots, like the Roomba - a programmable robotic vacuum which cleans floors on its own - are already commonplace in many US
households.</span></div></p>'
想清掉被<div> </div>包含的字
但<div>裡面有時候又會放一些屬性,
我試了好多個php regex還是都失敗, 拜託高手指點
my trial:
preg_replace("/<div[.*]>[.*]<\/div>/i","", $string);
ps. regex好難
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.21.229
1F:→ buganini:[]不是這樣用的 12/30 11:31
2F:→ buganini:/<div.*?>.*?<\/div>/ or /<div[^>]*>.*?<\/div>/ 12/30 11:32
3F:→ buganini:後者效能較好 12/30 11:32
4F:→ tnsshnews:我剛才試過完全沒有任何處理ㄟ....還是一樣在@@ 12/30 13:04
6F:→ tnsshnews:regex只能用try的? 12/30 14:41
7F:→ MOONRAKER:你現在不try就永遠只能用try的。 12/31 11:47
8F:→ MOONRAKER:踹了一下(這點我強調)發現,div後面不加上\s+就match 12/31 11:53
9F:→ MOONRAKER:不到,所以寫成土土的<(div\s+[^>]*)|(div)>.*?<\/div> 12/31 11:54
10F:→ MOONRAKER:…不對,要<((div\s+[^>]*)|(div))>.*?<\/div>才行 12/31 11:55
11F:→ chrisQQ:應該不是說用try的啦… 只是因為你的資料(看起來)很複雜 12/31 14:26
12F:→ chrisQQ:你可以用這個flash先一步一步的把 <div> </div> parse出來 12/31 14:26
13F:→ chrisQQ:在慢慢延伸變化到你想要的。另外,右邊有很多網友提供的 12/31 14:27
14F:→ chrisQQ:regex 可以套用,你可以試試看符不符合你的需求~ 12/31 14:27
15F:→ chrisQQ:不過說到底也是我沒有這麼熟悉regex啦,所以只好用try的QQ 12/31 14:29
16F:推 buganini:我忘了加modifier... 後面的/後面加個s吧 再加個i也可 01/06 19:38