SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Az
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Where to StripSlashes??

    Hi,

    I'm using the code below, found on here, to print last 5 entries of a text file. Having problems with using apostrophes and quotes. Searched here and found a solution offered by Freddy:
    $myString = stripslashes($myString);

    But I haven't been able to figure out where to use it to make it work!! I tried it using $posts = stripslashes($posts) right before the echo, and a few other places. Can someone help?? Thanks!


    [PHP<?php
    $file = "nyheter.txt";
    $f = fopen($file, "r");
    $s = fread($f,filesize($file));
    fclose($f);
    $posts=explode("\n",$s);
    $posts=array_reverse($posts, FALSE);
    for($i=0; $i<5; $i++) {
    echo $posts[$i];
    }
    ?>
    [/PHP]

    Here's the thread where I got the code, incase it's easier to change one of the other files:
    http://www.sitepointforums.com/showt...threadid=28883

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    Code:
    <?php 
    $file = \"nyheter.txt\"; 
    $f = fopen($file, "r"); 
    $s = fread($f,filesize($file)); 
    fclose($f); 
    $posts=explode("\n",$s); 
    $posts=array_reverse($posts, FALSE); 
    for($i=0; $i<5; $i++) { 
    echo $posts[$i]; 
    } 
    ?>
    Also do you want the \ before the n in
    $posts=explode("\n",$s); ?

    Sketch
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Az
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick response, thanks!
    Putting the slashes $file = \"nyheter.txt\"; gave me a php error: Warning: Unexpected character in input: '\' (ASCII=92) state=1

    As far as the \ before the n:
    no clue, honestly! Just using the code I found to have a form submit, which adds to a .txt file, then the page which the above code goes in is supposed to open, read, and print the last 5 entries in the text file. I did find that the above code is only listing the last 4 submission, I'd like about 10, but I think playing with the "5" in the above code will solve that. My use of it is to allow a user to put "events" on a page on my site.

  4. #4
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you try with the file() function? It automatically loads each line of the file into an array element,
    PHP Code:
    <?php 
     $file 
    "nyheter.txt"
     
    $filecontent file($file); 
     
    $filecontent array_reverse($filecontent);

     for(
    $i=0$i<5$i++)
        echo 
    $filecontent[$i]; 
    ?>
    BTW what's exactly your problem with quotes and apostrophes?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Az
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try the file() idea, thanks!!
    The problem with the apostophes and quotes is it prints as \'s or \"this

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
      echo stripslashes($filecontent[$i]); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arhh, to late.

    Anyway, JohnAz that's the missing code you need to get rid of those slashes.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A cat codes at the speed of light, its only the cat's owner who slows things down so much from slow-**** typing
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL (I should get one of those cats)

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Az
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all for the quick help!!!
    Problem solved!

    Discovered the real problem: I've got 3 dogs.
    (Dogs are stupid)


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
  •