SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Text file

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Text file

    I have a .txt-file chart.txt. I would like to add a 0 to all the 1 digit numbers so 1 become 01 and 2 become 02 and 3 become 03 and so on

    I tried something like this but it dosn´t quite work :-(

    PHP Code:

    $file 
    file(chart.txt);
    $num count($file);
    for (
    $i=0$i<$num$i++) {
    $a strpos($file[$i], "/")
    if (
    $a) { $b substr($file[$i],$a-1,1);
    $c substr($file[$i],$a-2,1);
    if (!
    is_numeric($c)) $c substr_replace($c,0,0,1)
    }

    chart.txt

    1/19/O-Zone/Dragostea din tei
    2/2/Mario Winans feat. P. Diddy/I don't wanna know
    3/1/C21/All that I want
    4/12/The Loft/City of dreams
    5/4/Maroon 5/This love
    6/3/Britney Spears/Everytime
    7/16/Nik & Jay/En dag tilbage
    8/6/The Underdog project/Saturday night
    9/10/Kelis/Trick me
    10/13/Johnny DeLuxe/Drømmer jeg
    11/17/Maria Mena /You're the only one
    12/7/Hoobastank/The reason
    13/18/Outlandish/Man binder os på mund og hånd
    14/15/Usher/Burn
    15/5/Beyonce/Naughty Girl
    16/11/Ana/We are

  2. #2
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way...
    PHP Code:
    $file file("chart.txt"); 
    $num count($file);
    for (
    $i 0;$i $num;$i++) 
    {
        
    $a strpos($file[$i], "/");
        if(
    $a 2)
        {
            
    $a "0".$file[$i];
        }
        else
        {
            
    $a $file[$i];
        }
        echo 
    "<br>".$a;

    Lats...

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That dosn´t quite work because i would also like for the second column of 1 digits numbers to get a 0 in front.

    The following line of the text
    PHP Code:
    3/1/C21/All that I want 
    should end up like
    PHP Code:
    03/01/C21/All that I want 
    With the current code only the first digit after the slash / gets a 0 in front

  4. #4
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, I missed the 'all single digit' bit

    Try this...
    PHP Code:
    $file file("chart.txt"); 
    $num count($file); 
    $format "%02d/%02d/%s/%s";
    for (
    $i 0;$i $num;$i++) 

        
    $list explode("/",$file[$i]);
        
    $a printf($format,$list[0],$list[1],$list[2],$list[3]);
        echo 
    "<br>".$a

    Lats...

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working with the following code. You got me in the right direction thanks a lot Lats for your time and help.

    PHP Code:
    $file file('thevoice/charts/45.txt');
    $num count($file);

    for (
    $i 0$i $num$i++) { 
    $b strpos($file[$i], "/"); 
    $y substr($file[$i],$b+2,1);
    $x substr($file[$i],$b+1,1);

    if(
    $b == 1) { 
    $a "0".$file[$i];


    else if (!
    is_numeric($y) && is_numeric($x)) { 
    $a substr_replace($file[$i],0,$b+1,0);



    else { 
    $a $file[$i]; 


    echo 
    $a "<br>";


    Last edited by abo2512; Nov 11, 2004 at 10:40.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,499
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Could be something like this?

    PHP Code:
    $file file("chart.txt"); 
    foreach(
    $file as $line_num => $line)
    {
        
    $cleaned_line strlen($line) - 1);
        
    strlen($line) - 1)list($number1$number2$artist$song) = explode("/"$cleaned_line);
        if (
    $number1 >= "0" && $number1 <= "9")
        {
            
    $number1 "0$number1";
        }
        if (
    $number2 >= "0" && $number1 <= "9")
        {
            
    $number2 "0$number2";
        }
        echo 
    "$number1/$number2/$artist/$song<br>";


  7. #7
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lats i will just stick to your code :-) It is much better than mine


Bookmarks

Posting Permissions

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