【问题标题】:Help with PHP data mangling as with sed/awk/grep与 sed/awk/grep 一样帮助 PHP 数据处理
【发布时间】:2010-04-30 06:28:01
【问题描述】:

好的,伙计们.. 我有一个 HTML,我需要将其解析为一个 php 脚本,并在 abit 周围破坏数据。为了获得最佳解释,我将展示如何在 bash 脚本中使用 awk、grep、egrep 和 sed 通过一组糟糕透顶的管道来执行此操作。为清楚起见进行了评论。

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 "onemyndseye@localhost"         | \ # 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: 
        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" />

并将其分解为完整的单行输入语句。准备插入另一种形式:

<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 卷曲页面……但似乎我在过滤输出时迷失了方向。

提前致谢。 :)

【问题讨论】:

    标签: php sed grep


    【解决方案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>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 2017-08-28
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多