SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Volitics's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to 1). see if string has "\r", "\r\n" line ending; 2). use 't' mode with fopen()?

    .
    Question 1: How can I get a PHP script to output the carriage return (i.e. "\r") and/or the carriage return\newline (i.e. "\r\n") characters in my browser window?

    I need to visually see whether a text file that I upload to my server hosting has lines that end with a carriage return ("\r") or a carriage return and a new line ("\r\n").

    I want to output the actual backslash(es) \ and letters n and/or r in the browser window where I can visually see them. Is there a PHP built-in function that will do that?

    Question 2a: What does the PHP Manual mean by "text-mode translation flag?"

    The PHP Manual has confusing information in the description for the fopen() function.

    It says that you should use the text-mode translation flag ('t') when working with plain text files. But below that it says that you should rewrite code that relies upon the 't' mode so that it uses the correct line endings.

    Question 2b: The list of modes "A list of possible modes for fopen() using mode" shown on the page does not offer the 't' mode. How do you incorporate the 't' mode into your script if the 't' mode is not offered with the fopen() function?

    I would appreciate if someone would help me clarify the issue with the r's, n's, and t's?

    Thanks.

    http://www.php.net/manual/en/function.fopen.php

    From the PHP Manual:

    Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

    The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.

    Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.
    .

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Question 1: How can I get a PHP script to output the carriage return (i.e. "\r") and/or the carriage return\newline (i.e. "\r\n") characters in my browser window?
    Escape the backslash:

    PHP Code:
    echo "\\n";
    echo 
    "\\r\\n"
    Denny Schlesinger
    web services

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Volitics View Post
    I want to output the actual backslash(es) \ and letters n and/or r in the browser window where I can visually see them. Is there a PHP built-in function that will do that?
    There's no function like that but you can easily convert \n and \r to readable forms with string substitution:
    PHP Code:
    $readableText strtr($sourceText, array("\n" => '\n'"\r" => '\r')); 
    This will work because in PHP \n and \r in double quotes mean newline and carriage return characters respectively while in single quotes they are literal characters \, n and r. If you want literal characters in double quotes you have to escape them like captainccs did it.

    Quote Originally Posted by Volitics View Post
    Question 2a: What does the PHP Manual mean by "text-mode translation flag?"

    The PHP Manual has confusing information in the description for the fopen() function.

    It says that you should use the text-mode translation flag ('t') when working with plain text files. But below that it says that you should rewrite code that relies upon the 't' mode so that it uses the correct line endings.
    My advice would be to never use any text translation flags because it works differently depending on the platform and so the code becomes less portable and less predictable. I'd say always use the b flag so that what you read and write is exactly what you tell PHP to. Personally, when I create text files I always use \n as the line ending. The windows notepad has problem with them but then I never use the windows notepad because it's crap - there are plenty free alternatives that work so much better.

    When reading files in PHP I always make sure that my code accepts both \n and \r\n as the line ending and this way I don't deal with the unpredictable fopen flags - b is the only predictable flag and it basically says: "don't change anything in my data!".

    Quote Originally Posted by Volitics View Post
    Question 2b: The list of modes "A list of possible modes for fopen() using mode" shown on the page does not offer the 't' mode. How do you incorporate the 't' mode into your script if the 't' mode is not offered with the fopen() function?

    I would appreciate if someone would help me clarify the issue with the r's, n's, and t's?
    You are confusing two kinds of flags. The list you are referring to is actually not a flag but mode. The flags b and t can be optionally appended to mode. So you can have combinations like this:

    r
    rb
    rt
    r+
    r+b
    r+t
    w
    wb
    wt


    and so on.

  4. #4
    SitePoint Addict Volitics's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    captainccs, Lemon Juice;

    Thanks for responding to my questions. The answers give me some information to work with. Thanks again.
    .


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
  •