[請益]要怎樣將特定的html tag所包夾的字串清掉

看板PHP作者 (tnsshnews)時間15年前 (2010/12/30 11:10), 編輯推噓1(1015)
留言16則, 4人參與, 最新討論串1/1
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

12/30 11:31, , 1F
[]不是這樣用的
12/30 11:31, 1F

12/30 11:32, , 2F
/<div.*?>.*?<\/div>/ or /<div[^>]*>.*?<\/div>/
12/30 11:32, 2F

12/30 11:32, , 3F
後者效能較好
12/30 11:32, 3F

12/30 13:04, , 4F
我剛才試過完全沒有任何處理ㄟ....還是一樣在@@
12/30 13:04, 4F

12/30 13:34, , 5F
http://www.gskinner.com/RegExr/ 先嘗試出正確的regex~
12/30 13:34, 5F

12/30 14:41, , 6F
regex只能用try的?
12/30 14:41, 6F

12/31 11:47, , 7F
你現在不try就永遠只能用try的。
12/31 11:47, 7F

12/31 11:53, , 8F
踹了一下(這點我強調)發現,div後面不加上\s+就match
12/31 11:53, 8F

12/31 11:54, , 9F
不到,所以寫成土土的<(div\s+[^>]*)|(div)>.*?<\/div>
12/31 11:54, 9F

12/31 11:55, , 10F
…不對,要<((div\s+[^>]*)|(div))>.*?<\/div>才行
12/31 11:55, 10F

12/31 14:26, , 11F
應該不是說用try的啦… 只是因為你的資料(看起來)很複雜
12/31 14:26, 11F

12/31 14:26, , 12F
你可以用這個flash先一步一步的把 <div> </div> parse出來
12/31 14:26, 12F

12/31 14:27, , 13F
在慢慢延伸變化到你想要的。另外,右邊有很多網友提供的
12/31 14:27, 13F

12/31 14:27, , 14F
regex 可以套用,你可以試試看符不符合你的需求~
12/31 14:27, 14F

12/31 14:29, , 15F
不過說到底也是我沒有這麼熟悉regex啦,所以只好用try的QQ
12/31 14:29, 15F

01/06 19:38, , 16F
我忘了加modifier... 後面的/後面加個s吧 再加個i也可
01/06 19:38, 16F
文章代碼(AID): #1D6_WOwz (PHP)
文章代碼(AID): #1D6_WOwz (PHP)