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
ewline (i.e. "\r
") 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
").

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 [COLOR=“#800000”]fopen/COLOR 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 [COLOR=“#800000”]fopen/COLOR 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 [COLOR=“#800000”]fopen/COLOR 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
to \r
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
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.

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

Escape the backslash:

echo "\\\
";
echo "\\\\r\\\
";

There’s no function like that but you can easily convert
and \r to readable forms with string substitution:


$readableText = strtr($sourceText, array("\
" => '\
', "\\r" => '\\r'));

This will work because in PHP
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.

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
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
and \r
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!”.

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.

captainccs, Lemon Juice;

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