note 77820 added to function.php-strip-whitespace
| From: | ZvjezdanPatz at osu1 dot php dot net | Date: | Fri, 14 Sep 2007 03:14:16 +0000 |
| Subject: | note 77820 added to function.php-strip-whitespace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130487@lists.php.net to get a copy of this message | ||
I was given a report that was separated by spaces and asked to make graphs from it. I needed to
turn the report data into a csv in memory so I could manipulate it further.
First needed to see the report, then need to strip out the whitespace, but leave one space between
each item that I could convert to a column.
There were lots of complicated ways to do this. I stumbled on something simple.
Say the report looks like this:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
1 5 7 8 10 7 8
7 15 4 0 21 4 12
9 5 7 9 0 9 43
The report is using spaces and not tabs to separate everything. Assume it's a file called
data.txt you can use the following to strip out the spaces and make it comma delimited:
<?php
$handle = @fopen("data.txt", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
// this will search for 5 spaces and replace with 1, then 4, then 3, then 2
// then only one will be left. Replace that one space with a comma
// then output with nl2br so you can see the line breaks
print nl2br(str_replace(" ", ",",ereg_replace( ' ', '
',ereg_replace( ' ', ' ', ereg_replace( ' ', ' ',
ereg_replace( ' ',' ',$buffer ))))));
}
}
fclose($handle);
?>
Hope that helps someone else.
----
Server IP: 216.235.15.211
Probable Submitter: 68.147.110.13
----
Manual Page -- http://www.php.net/manual/en/function.php-strip-whitespace.php
Edit -- https://master.php.net/note/edit/77820
Del: integrated -- https://master.php.net/note/delete/77820/integrated
Del: useless -- https://master.php.net/note/delete/77820/useless
Del: bad code -- https://master.php.net/note/delete/77820/bad+code
Del: spam -- https://master.php.net/note/delete/77820/spam
Del: non-english -- https://master.php.net/note/delete/77820/non-english
Del: in docs -- https://master.php.net/note/delete/77820/in+docs
Del: other reasons-- https://master.php.net/note/delete/77820
Reject -- https://master.php.net/note/reject/77820
Search -- https://master.php.net/manage/user-notes.php