Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Daily Fact Script Help

    Basically, it is supposed to open "quotes.txt", explode and find the current date (in Ymd format), then echo the second exploded part of the matching date. I just can't seem to find the problem with it. I've included my quotes.txt and php file below:
    Code:
    20090911|That my script works!
    20090912|That my script works! (again) YAY.
    PHP Code:
    $current date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
    $file fopen("quotes.txt","r");
    $explode explode('|'$file); // [0] date, [1] quote
    $strpos strpos($current$explode[0]);
    if (
    $strpos == TRUE) { 
      echo 
    "Did you know... " $explode[1];
    }
    else {
      echo 
    "error";


  • #2
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    This should work:
    PHP Code:
    $current date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
    $file file("quotes.txt");
    foreach(
    $file as $key => $value){
    $strpos strpos($current$key);
    if (
    $strpos == TRUE) { 
      echo 
    "Did you know... " $value;
    }
    else {
      echo 
    "error";
    }


  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Without testing it, something like this perhaps:

    PHP Code:
    $success false;
    $file file_get_contents("quotes.txt"); 
    $lines preg_split('/\n|\r/'$file, -1PREG_SPLIT_NO_EMPTY); 
    foreach (
    $lines as $line)
    {
       
    $line_parts explode('|'$line)
       if (
    is_array($line_parts) && count($line_parts) > 1)
       {
           if (
    $line_parts[0] == date("Ymd"))
           {
              echo 
    "Did you know... " $line_parts[1];
              
    $success true;
              break;
           }
       }
    }

    if (!
    $sucesss)
    {
        die(
    'Error');



    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Quote Originally Posted by sea4me View Post
    This should work:
    PHP Code:
    $current date("Ymd"); //YearMonthDay (20090321 - March 21st, 2009)
    $file file("quotes.txt");
    foreach(
    $file as $key => $value){
    $strpos strpos($current$key);
    if (
    $strpos == TRUE) { 
      echo 
    "Did you know... " $value;
    }
    else {
      echo 
    "error";
    }

    looks like file() will also work with the optional flags for PHP5:

    PHP Code:
    $success false;
    $lines file("quotes.txt"FILE_IGNORE_NEW_LINES FILE_SKIP_EMPTY_LINES); 
    foreach (
    $lines as $line)
    {
       
    $line_parts explode('|'$line)
       if (
    is_array($line_parts) && count($line_parts) > 1)
       {
           if (
    $line_parts[0] == date("Ymd"))
           {
              echo 
    "Did you know... " $line_parts[1];
              
    $success true;
              break;
           }
       }
    }

    if (!
    $sucesss)
    {
        die(
    'Error');

    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •