[nycphp-talk] table to tab-delimit
Adam Maccabee Trachtenberg
adam at trachtenberg.com
Mon Oct 13 16:58:29 EDT 2003
On Mon, 13 Oct 2003, Jay Greenspan wrote:
> How would you go about converting an HTML table to tab-delimited text.
> assuming same number of columns for each row and that there will be
> attributes within the table tags.
1) Slurp table into variable
2) split() on <tr>s
3) iterate over each line doing:
3.1) preg_match_all() on /<td[^>]*>(.+)</td>/U or something similar,
making sure to use non-greedy capturing patterns
3.2) join() each capture item using \t as your delimiter
3.3) store the results in an array: $lines[] = join('\t', $matches)
(or something like that)
4) save results to file
4.1) open file
4.2) write each array element as line, placing \n at end
4.3) close file
Depending upon the frequency you do this, you may find this easier in
a text editor that supports regex matching. Or not.
-adam
--
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!
More information about the talk
mailing list