tags:

views:

203

answers:

1

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

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"&gt;
        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"&gt;
        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"&gt;http://www.linux.com/rss/feeds.php&lt;/a&gt; <br />
<input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml"&gt;http://www.ubuntu.com/rss.xml&lt;/a&gt; <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  A: 

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>';
Byron Whitlock
Thanks - that was a nice point in the right direction. :)
onemyndseye