Function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
wattee
Forum Newbie
Posts: 12
Joined: Mon Nov 02, 2009 5:00 am

Function help

Post by wattee »

Hello!

1st of all yes im a new user, this forum looks really active and enjoyable, but the truth is i wouldn't have joined
if i didn't need any help, since im new in PHP and got some difficulties.

Problem:
I have a simple text file where there's some usual text with commas, spaces and dots lines separated by \n.

I have this function:
------------------------------------------------

Code: Select all

$sfile = 'test.txt';
function fcontent($sfile)
{
    $fc = file_get_contents($sfile);
    $del = "\n";
    $arr = explode($del, $fc);
    $fp = fopen("log.html", "w+");
    
    foreach($arr as $key => $value)
    {
        fwrite($fp, $value . "\n");
    }
}
------------------------------------------------
Point of this function would be then taking file content from text file, putting it in to array and writing it to html file.
Everything works, but as you might guess it's not final.

1. The file log.html appears totally w/o any html elements file wich is logical since i haven't added any html elements, this is the 1st task thing i would need help with to insert into html file <table><tr><td></td>Some text here</tr></table>.

2. The second aim for this function woul'd be to write to file as an array value.
e.g. <tr><td>. $arr[1] .</td></tr>
So if i had somewhat 5 values each in a different row,
function been executed for the 3rd time, it would look something like this.

Code: Select all

 
<tr>
<td>. $arr[11] .</td>
<td>. $arr[12] .</td>
<td>. $arr[13] .</td>
<td>. $arr[14] .</td>
<td>. $arr[15] .</td>
</tr>
 
I know its alot to ask so if you would at least point me in the right direction i'd be thankful.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Function help

Post by McInfo »

It was easier to write a script than to explain how it should be done, so here's the script.

Code: Select all

<?php
 
// Loads lines of the file into an array
// Uses a bitwise "or" operator to combine flags
$lines = file('./input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 
// Converts the array to a string and adds a string separator between elements
$rows = implode('</td></tr><tr><td>', $lines);
 
// Builds an HTML string using heredoc syntax
// Output buffering could be used instead
$html = <<<HTML
<html>
    <head>
        <title>Data</title>
    </head>
    <body>
        <table border="1">
            <tbody>
                <tr><td>{$rows}</td></tr>
            </tbody>
        </table>
    </body>
</html>
HTML;
 
// Outputs the HTML
echo $html;
 
// Saves the HTML to a file
file_put_contents('./output.html', $html);
<ignore reason="function names not linked">Click on a function name to go to the manual page for that function.</ignore>

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 1:21 pm, edited 1 time in total.
User avatar
wattee
Forum Newbie
Posts: 12
Joined: Mon Nov 02, 2009 5:00 am

Re: Function help

Post by wattee »

Nice, never would've guessed it could be done like this.
Thanks, i almost gave up :D
Post Reply