Web Design and Web Development Forum

  1. #1
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    7
  2. ktsirig is on a distinguished road
  3. input record separator

    Hi all!
    I have written some scripts in Perl, where I make use of the $/ ,the input record separator.
    My question is whether such a thing exists also in PHP, and, if so, how can I separate multiple records in a text file, that are separated (let's say by //).

    In Perl I write: $/="//";

    In PHP????
    Reply With Quote Reply With Quote
  4. #2
    Atolar's Avatar
    Join Date
    Jun 2005
    Posts
    690
    Rep Power
    7
  5. Atolar is on a distinguished road
  6. Re: input record separator

    woa. sry, i know nothing about perl.

    EDIT: if you mean like when ur inserting stuff through mysql, you seperate the values with ',' as in... insert values('value1', 'value2')
    Last edited by Atolar; 10-15-2005 at 03:17 PM.
    Reply With Quote Reply With Quote
  7. #3
    unclekyky's Avatar
    Join Date
    Sep 2004
    Age
    22
    Posts
    5,184
    Rep Power
    13
  8. unclekyky is on a distinguished road
  9. Re: input record separator

    try the function explode('//', $text);

    more at php.net
    Spore-Game - The Ultimate Spore Fan-Site
    Abnegating Avunculicide Since 1601 | YC Wiki - "Quidquid latine dictum sit, altum sonatur."
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0
  11. christopherspaulin is on a distinguished road
  12. Re: input record separator

    The goal is to read lines in a file separated by something other than a new line character.

    Instead of using this Perl code:

    my $old_input_line_separator = $/; # saved
    $/ = '<hr>'; # switch to <hr>, which separates records in a text file
    ...Read records from a file...
    $/ = $old_input_line_separator; # restored

    You can use this PHP code to get the second record in the file, file_name.txt, where each record is separated by <hr>. Each record has multiple lines. <hr> is on its own line:

    $file = '/directory/subdirectory/file_name.txt';
    $file_handle = fopen($file, 'r');
    $count = 0;
    while ($line = stream_get_line($file_handle, 0, '<hr>'))
    {
    ++$count; // Increase the count by 1.
    if ($count == 2)
    {
    echo "$line";
    break;
    }
    }
    fclose($file_handle);

    Explanation:

    The PHP function stream_get_line() has a parameter (third parameter) to separate a line by your own separator instead of a new line character.

    string stream_get_line ( resource $handle , int $length [, string $ending ] )

    I used 0 for the length (second parameter) so that it wouldn't use the length to separate the lines, but only use the line separator (third parameter). It reads a line until it comes to '<hr>'. The '<hr>' is not included in the line that it returns. The first parameter, the resource handle, comes from fopen() when I opened the file.

    Reading ends when length bytes have been read, when the string specified by ending is found (which is not included in the return value), or on EOF (whichever comes first).

    See PHP: Hypertext Preprocessor for more information on stream_get_line().
    Reply With Quote Reply With Quote

Similar Threads

  1. Oil swirls to a record
    By extravirgin in forum Chit Chat and Hangout
    Replies: 23
    Last Post: 09-03-2005, 02:28 PM
  2. sensoring input forms
    By Atolar in forum PHP Scripting
    Replies: 8
    Last Post: 08-01-2005, 11:44 AM
  3. Input Boxes
    By asgsoft in forum JavaScript Coding
    Replies: 2
    Last Post: 06-06-2005, 12:24 PM
  4. Please Help
    By jimmyboyarcher in forum C and C++ Programming
    Replies: 6
    Last Post: 06-05-2005, 12:21 PM
  5. Input Box with domain
    By solmonsk8er in forum PHP Scripting
    Replies: 6
    Last Post: 03-04-2005, 02:53 AM