像sed / awk / grep一样帮助PHP数据修改

Ok guys.. I have a HTML i need to parse into a php script and mangle the data around abit. For best explanation I will show how I would do this in a bash script using awk, grep, egrep, and sed through a god awful set of pipes. Commented for clarity.

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/
//gp;N;b a" | \ # Remove every line break
       sed '{s|<br />|<br />
|g}'          | \ # Insert new line breaks after <br />
       grep "onemyndseye@localhost"         | \ # Get lines containing my local email
       sed  '{s/\[[^|]*\]//g}'              | \ # Remove my email from the line

These commands take the form element that looks like this:

<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: 
        onemyndseye@localhost (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: 
        onemyndseye@localhost (Default)
    ]<br /> 
    <input type="submit" name="delete_submit" value="Delete Selected" />

And mangles it into complete one-line input statements.. Ready to be inserted into another form:

<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 />

The big question is how to accomplish this in PHP? I am comfortable with using PHP to curl a page... but it seems I am lost on filtering the output.

Thanks in advance. :)

1个回答



您不过滤输出。 您使用 simple_html_dom 来解析和操纵这种方式。 它确实更直观。</ p>

类似</ p>

  //从URL或文件创建DOM 
$ html = file_get_html('。 ..');

//找到表格标签
foreach中的所有href($ html-&gt; find('form a')为$ element)
echo $ element-&gt; src。 '&lt; br&gt;';
</ code> </ pre>
</ div>

展开原文

原文

You don't filter output. You use simple_html_dom to parse and manipulate that way. it really is more intuitive.

Something like

// 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>';

doublestar2014
doublestar2014 谢谢 - 这是朝着正确方向迈出的一个好点。 :)
大约 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问