2010-04-29 40 views
1

好吧,我有一個HTML我需要解析爲一個php腳本,並將數據轉移到abit中。爲了得到最好的解釋,我將演示如何在bash腳本中使用awk,grep,egrep和sed通過一組神奇可怕的管道來做到這一點。評論清晰。與sed/awk/grep一起幫助PHP數據加密

curl -s http://myhost.net/mysite/   | \ # retr the document 
     awk '/\/\action/,/submit/'   | \ # Extract only the form element 
     egrep -v "delete|submit"    | \ # Remove the action lines 
     sed 's/^[ \t]*//;s/[ \t]*$//'  | \ # Trim extra whitespaces etc. 
     sed -n -e ":a" -e "$ s/\n//gp;N;b a" | \ # Remove every line break 
     sed '{s|<br />|<br />\n|g}'   | \ # Insert new line breaks after <br /> 
     grep "[email protected]"   | \ # Get lines containing my local email 
     sed '{s/\[[^|]*\]//g}'    | \ # Remove my email from the line 

這些命令採取看起來像這樣的表單元素:

<form action="/action" method="post"> 
    <input type="checkbox" id="D1" name="D1" /><a href="http://www.linux.com/rss/feeds.php"> 
     http://www.linux.com/rss/feeds.php 
    </a> [email: 
     [email protected] (Default) 
    ]<br />   
    <input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml"> 
     http://www.ubuntu.com/rss.xml 
    </a> [email: 
     [email protected] (Default) 
    ]<br /> 
    <input type="submit" name="delete_submit" value="Delete Selected" /> 

它軋液成完整的一行輸入的語句..準備被插入到另一種形式:

<input type="checkbox" id="D1" name="D1" /><a href="http://www.linux.com/rss/feeds.php">http://www.linux.com/rss/feeds.php</a> <br /> 
<input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml">http://www.ubuntu.com/rss.xml</a> <br /> 

最大的問題是如何在PHP中做到這一點?我很喜歡使用PHP來捲曲頁面......但似乎我迷失在過濾輸出。

在此先感謝。 :)

回答

1

您不過濾輸出。您使用simple_html_dom來解析和操作。它確實更直觀。

喜歡的東西

// Create DOM from URL or file 
$html = file_get_html('...'); 

// Find all a hrefs in a form tag 
foreach($html->find('form a') as $element) 
     echo $element->src . '<br>'; 
+0

感謝 - 這是在正確方向上的好看點。 :) – onemyndseye 2010-04-30 10:52:30