note 27470 modified in function.fopen by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 23:46:54 +0000 |
| Subject: | note 27470 modified in function.fopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156572@lists.php.net to get a copy of this message | ||
I just used explode() as an alternative to fscanf, since my only delimiter was | (pipe). I was
having problems with it, since I use it in my news-management script. I found that it cut the last
variable I was using, $body, a bit short when I posted a long news post. This would've been a
real problem for anybody trying to make news posts longer than a paragraph or so.
However, I found that when I used:
<?php list($variable1, $variable2, etc) = explode("|",$data); ?>
it didn't cut any variables short, so.. what I'm really trying to say here is that for
people who are experiencing problems with parsing simple files (i.e with only a single, simple
delimiter such as : or |) using the unecessarily complex fscanf() and sscanf() functions, explode()
is definately the way to go.
<?php
function get_news($filepath, $newsid)
{
$datafile = fopen("$filepath/news/$newsid.txt","r");
$data = fread($datafile, 1000000);
list($author, $email, $date, $subject, $body) = explode("|",$data);
$body = stripslashes("$body");
$subject = stripslashes("$subject");
echo "<a href=\"mailto:$email\">$author</a> -- $date --
$subject<hr>$body<p>";
}
?>
sample file:
AdministratorMax|admin@example.com|Tuesday, March 5th @ 5:45 PM EST|Site Going Down Tomarrow|Well,
folks, I\'m sorry to say that the site will indeed be down tomarrow for most of the day. Hang
in there.
Output:
<a href="mailto:admin@example.com">AdministratorMax</a>
-- Tuesday, March 5th -- Site Going Down Tomarrow<hr>Well, folks, I'm sorry to say that
the site will indeed be down tomarrow for most of the day. Hang in there.
Thought that might be useful for anybody making a simple news-management script, ;)
By the way, feel free to correct me if I made any mistakes - I'm at my dad's work where I
don't really have a way to check to see if it works or not. However, I use a more complex
version of this on my portal project, and it works beautifully.
--was--
I just used explode() as an alternative to fscanf, since my only delimiter was | (pipe). I was
having problems with it, since I use it in my news-management script. I found that it cut the last
variable I was using, $body, a bit short when I posted a long news post. This would've been a
real problem for anybody trying to make news posts longer than a paragraph or so.
However, I found that when I used:
list($variable1, $variable2, etc) = explode("|",$data);
it didn't cut any variables short, so.. what I'm really trying to say here is that for
people who are experiencing problems with parsing simple files (i.e with only a single, simple
delimiter such as : or |) using the unecessarily complex fscanf() and sscanf() functions, explode()
is definately the way to go.
function get_news($filepath, $newsid)
{
$datafile = fopen("$filepath/news/$newsid.txt","r");
$data = fread($datafile, 1000000);
list($author, $email, $date, $subject, $body) = explode("|",$data);
$body = stripslashes("$body");
$subject = stripslashes("$subject");
echo "<a href=\"mailto:$email\">$author</a> -- $date --
$subject<hr>$body<p>";
}
sample file:
AdministratorMax|admin@somesite.com|Tuesday, March 5th @ 5:45 PM EST|Site Going Down Tomarrow|Well,
folks, I\'m sorry to say that the site will indeed be down tomarrow for most of the day. Hang
in there.
Output:
<a href="mailto:admin@somesite.com">AdministratorMax</a>
-- Tuesday, March 5th -- Site Going Down Tomarrow<hr>Well, folks, I'm sorry to say that
the site will indeed be down tomarrow for most of the day. Hang in there.
Thought that might be useful for anybody making a simple news-management script, ;)
By the way, feel free to correct me if I made any mistakes - I'm at my dad's work where I
don't really have a way to check to see if it works or not. However, I use a more complex
version of this on my portal project, and it works beautifully.
http://php.net/manual/en/function.fopen.php