How to echo text from character 2-27 in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giorgisp
    New Member
    • May 2010
    • 6

    How to echo text from character 2-27 in a file

    I have a file and I want to echo the text from character 2 to 27. How can I do this?

    Thanks
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You can do it with PHP. But I can't just give you the code.

    @ bytes we like the user to fully understand the code they are producing.

    So I'll point you in the right direction and see if you can write some code. If it doesn't do what you expect or doesn't work, come back here and ask us!

    Depending on the size of the file, if small see http://php.net/file, if it's large see http://php.net/fopen , fread, etc.

    Then you must have a loop. A while loop or for, foreach loop.

    To get the characters you want, use this handy dandy function called sub string: http://us.php.net/substr

    I hope that gives you a good start. Let us know!






    Dan

    Comment

    • giorgisp
      New Member
      • May 2010
      • 6

      #3
      Thanks for the reply. The file has in each line an email address and some text next to it seperated with a space. So I give the email address I want to the script with the get method (url variable) and I want it to echo the email address along with the text next to it (from the start of the line to the end of the line). I just wrote this code to do this but it didn't work as I expected. Sometimes it gives me back more characters than what I want.

      Code:
      <?php
      
      $break = "\n";
      $textfile = "cake";
      $file = fopen("$textfile", 'r');
      $data = fread($file, filesize($textfile));
      fclose($file);
      
      $start = strpos($data, $_GET["email"]);
      $end = strpos($data, $break, $start );
      
      echo substr($data, $start, $end);
      
      ?>

      Comment

      • giorgisp
        New Member
        • May 2010
        • 6

        #4
        Oh, I found what I was doing wrong! substr() has parameters on where to search, start and how much characters to return from the start character, not the end character. So the updated code is:

        Code:
        <?php
        
        $break = "\n"; //set the line break character
        $textfile = "cake"; //set the file we want to read
        $file = fopen("$textfile", 'r'); // open the file in read mode
        $data = fread($file, filesize($textfile)); // read the whole file and save it in a variable
        fclose($file); //close the file
        
        $start = strpos($data, $_GET["email"]); //find the first character's position of the string given in the url
        $end = strpos($data, $break, $start ); //find the position of the last character in the line
        
        $end1 = $end - $start; //subtract the end character from the start character to find the string lenght and save it in a variable
        
        echo substr($data, $start, $end1); //use substr to trim the file contents and provide it with the file content, the starting character and the lenght and echo the part/line we want
        
        ?>
        And it works perfectly!

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Glad you got it working!






          Dan

          Comment

          Working...