- Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
- Questions : Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
- Answers 1 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
- Answers 2 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
- Answers 3 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
- PHP remove line break or CR LF with no success
- PHP remove line break or CR LF with no success
- How to manipulate CRLF in php?
- Check if string contain ( CR, LF or CF or LF) or not using PHP
- Can I avoid CRLF injection attacks by replacing JUST the CR?
- Multiple Lines breaks removing
- Recommended Answers Collapse Answers
- All 9 Replies
Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
Questions : Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
In different text files (or also inside query uvdos php single text file) I have different query uvdos php end-of-lines combinations (see example query uvdos php below).
How to uniform all combinations of CR and LF query uvdos php with one simple CRLF? In a few words I need query uvdos php to replace every CR and LF combinations with query uvdos php one single CRLF using PHP. With str_replace query uvdos php I can replace them but my issue is the right query uvdos php search string to use.
$textfile=str_replace("search string i need","CRLF to replace", $textfile);
Example of a generic text file to fix:
text line 1 CRLFLFCRCRLF text line 2 LFLFCRLFCRCR text line 3 CRLF text line 4 CR text line 5 LF
I need to replace all the \r \n random query uvdos php combinations with only \r\n like this:
text line 1 CRLF text line 2 CRLF text line 3 CRLF text line 4 CRLF text line 5 CRLF
Answers 1 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
PCRE has an alias for any newline help uvdos preg-replace combination: \R
$text = preg_replace('~\R~', "\r\n", $text);
In 8 bit mode, \R matches CR, LF, or help uvdos preg-replace CRLF, but also the vertical tabulation help uvdos preg-replace (VT), the form feed (FF) and the next help uvdos preg-replace line character (NEL). In other words, \R help uvdos preg-replace is an alias for help uvdos preg-replace (?>\r\n|\n|\x0b|\f|\r|\x85). But help uvdos preg-replace since VT, FF and NEL are rarely (never?) help uvdos preg-replace used todays. However, it’s possible to help uvdos preg-replace restrict \R to only CR, LF and CRLF help uvdos preg-replace using (*BSR_ANYCRLF) at the start of the help uvdos preg-replace pattern:
$text = preg_replace('~(*BSR_ANYCRLF)\R~', "\r\n", $text);
if you want to extend the meaning of \R help uvdos preg-replace to any unicode newline sequences, use help uvdos preg-replace the u modifier:
$text = preg_replace('~\R~u', "\r\n", $text);
Concretly it adds the Line Separator help uvdos preg-replace U+2028 and the Paragraph Separator help uvdos preg-replace U+2029 to the list of newline sequences.
Take care that \R is an alias and not a help uvdos preg-replace shorthand character class. So you can’t help uvdos preg-replace put it inside a character class.
It can be interesting to use the intl help uvdos preg-replace transliterator instead of a simple help uvdos preg-replace replacement function with regex or not, help uvdos preg-replace in particular if you need to include help uvdos preg-replace other modifications to your strings. All help uvdos preg-replace of them can be centralized in a unique help uvdos preg-replace set of rules:
$tls = Transliterator::createFromRules('[\r\n]+ > \r\n;'); $text = $tls->transliterate($text);
Answers 2 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
To replace all combinations of ‘\r\n’ help uvdos preg-replace with ‘\r\n’ use:
$result = preg_replace('/[\r\n]+/', "\r\n", $text);
This will also replace single ‘\r’ or help uvdos preg-replace ‘\n’ with ‘\r\n’.
Answers 3 : of Replace any combination of (CR) and (LF) with a single (CRLF) in a text file using php
You don’t really need regex for that:
This will replace every one of the help uvdos preg-replace strings in the array (and keep the help uvdos preg-replace order, so if you have \r\n it will not help uvdos preg-replace be replaced to \r\n\r\n).
PHP remove line break or CR LF with no success
For example: Read the official documentation http://php.net/manual/ru/function.trim.php Question: Let us say I have string like below $comments = Windows Embedded Compact (Windows CE) or Windows Server. Solution 3: How about the following the first part will replace any line breaks, it’s leading spaces and it’s tailing spaces into just one space.
PHP remove line break or CR LF with no success
I did a function to remove line break with php with no success, i tryed all replace code and i still get these line break, i create a json file and i can’t read it from Jsonp with jquery because of these line break seem to break it all.
When i look at the source, there some line break appening in all href, img and br this is a json_encode output example:
line break afer a. it’s hapenig to img src and br
the only way i can remove these break it with
$text = preg_replace("/\s/i", '', $text);
But you understant that there’s no space left in all the string and it’s not what we want.
this replace works better for me:
= str_replace (array("\r\n", "\n", "\r"), ' ', $text)
function clean($text) < $parts = explode(' ', $text); foreach ($parts as $key =>$value) $parts[$key] = preg_replace('/\s/', ' ', $value); return implode(' ', $parts); >
Indeed, if instead of cleaning the JSON file like this, you can use json_encode to create it, you will get rid this problem in a previous step.
the first part \s*[\r\n]+\s* will replace any line breaks, it’s leading spaces and it’s tailing spaces into just one space.
the second part \s+ will shrink spaces into one space.
then trim() removes the leading/tailing space.
Try to use default trim function with «character_mask».
Read the official documentation http://php.net/manual/ru/function.trim.php
PhpStorm + GIT line endings changing from LF to CRLF, It seems it didn’t help at all. I’m still getting CRLF in my files. I looked at Version controle console and I see there git -c core.quotepath=false config core.autocrlfand for example git -c core.quotepath=false add —ignore-errors — tests/api/* warning: LF will be replaced by CRLF in tests/api/* The file will have its …
How to manipulate CRLF in php?
Let us say I have string like below $comments = Windows Embedded Compact (Windows CE) or Windows Server. Defunct Windows families include windows 9x and Windows Mobile. [CR][LF] [CR][LF] Microsoft introduced an operating environment named Windows on November 20, 1985, as a graphical operating system shell for MS-DOS in response to the growing interest in graphical user interfaces (GUIs).
Note: [CR][LF] are manually added for the question. I found this issue when any user copy and paste any html content in the comments rich text area box we are observing these elements.
Is there way where we can replace these with \n\r in PHP.
I tried something like this
$comments = preg_replace("/\r\n\r\n|\r\r|\n\n/", "
", $comments);
it didn’t give me any proper result.
Assuming you want to replace any combinations of [CR] and [LF] by [CR][LF] (i.e. \r\n , not \n\r ), try this:
$string = ". [CR]. [LF]. [CR][LF]. [CR][CR]. [LF][LF]. "; $result = preg_replace('/\r\n|\n\r|\r|\n/', '\r\n', $string);
Check if string contain ( CR, LF or CF or LF) or not using, I’m not a beginner in php! i have about two years experience! i know, there maybe dozen of ways to improve the function. i just wanted to answer quickly: – try2fly.b4ucry Dec 6, 2011 at 12:04
Check if string contain ( CR, LF or CF or LF) or not using PHP
I have a string and I want to control the content of the string it
contain ( CR, LF or CRLF) or not?
if (no CR, LF or CF or LF) in string: echo 'no sepator'; else: echo 'have some separator';
Anyone here could tell me the ways to do this in php
function hasNewLine($str) < return (strpos($str, "\n") !== false) || (strpos($str, "\r") !== false); >var_dump(hasNewLine("my stringn\n\rmy string"));
if you are looking for \n in a \n\r you don’t need to look for \n\r since the \n is already in the \n\r string.
this function checks that input string has any combination of new line characters, or not.
function hasNewLine($str) < $found = false; foreach (array("\r", "\n", "\r\n", "\n\r") as $token) < if (strpos($str, $token) !== false) < $found = true; break; >> return $found; >
and there’s another version of this function which detects the given string ends with new line characters:
function endsWithNewLine($str) < $found = false; foreach (array("\r", "\n", "\r\n", "\n\r") as $token) < $desiredPos = strlen($str) - strlen($token); if (strpos($str, $token) === $desiredPos) < $found = true; break; >> return $found; >
How to Create a New Line in PHP, Another important thing to mention: the \n character can write a new line within UNIX. At the same time, for Windows, the two character sequences exists like this: \r\n.So, we recommend you to use \r\n.
Can I avoid CRLF injection attacks by replacing JUST the CR?
I have a form which allows one file attachment, and generates an email to a hard-coded address. I would like to avoid the possibility of malicious users entering custom mail headers (a CRLF injection, so called since email headers end in \r\n as per RFC).
Suppose I run the following function on every piece of data which might make it into the $additional_headers parameter:
That replaces JUST the carriage return half of the CRLF pair. Will that prevent potential attacks adequately?
Ordinarily I would just replace the \r\n with an empty string. But this particular form allows for one attachment, which means that the message body actually winds up getting passed through the $additional_headers parameter since PHP doesn’t have a native function for building a multipart MIME encoded email (that I know of).
In case anyone cares, here’s my function for mailing an attachment:
if($filetype) < $filetype = strip_crlf($filetype); >// $to and $subject escaping handled natively by mail(); // $file is base64 encoded before mail_attachment() is called. $header = ''; // No file attachment; just send a regular email. if(!$file)< $header .= "From: ".$from_name." \r\n"; return mail($to, $subject, $message, $header); > $uid = md5(uniqid(time())); // Build a MIME encoded message. $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--$uid\r\n"; $header .= "Content-type:text/plain; charset=utf-8\r\n"; $header .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; $header .= "$message\r\n\r\n"; $header .= "--$uid\r\n"; $header .= "Content-Type: $filetype; name=\"$filename\"\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n"; $header .= "$file\r\n\r\n"; $header .= "--$uid--"; // Send the mail. return mail($to, $subject, '', $header); > ?>
If you’re worried about injections, then don’t build your own messages. Use Swiftmailer or PHPMailer, which take care of all those worries for you.
No, replacing just the CR is not sufficient — there are enough mail clients which only look at the LF that it can be exploited. Of course, most of the header fields don’t need newlines at all , so you can simply strip both CR and LF from everything but $message . For $message , either make sure it can’t contain your MIME seperator ( —$uid in this case), or encode it as base64 or something.
PHP — how to create a newline character?, note: using a feature rich text editor you should be able to insert a newline as a binary character(s) that represents a newline on a different operating system than the one editing the file. If all else fails, simply using a hex editor to insert the binary ascii character would do.
Multiple Lines breaks removing
I have tried every solution I have come across and am still not getting any results. I have a comment box and I am trying to remove all repetative new lines or /r/n from the input.
if (isset($_POST['comment'])) < $comment = mysql_real_escape_string($_POST['comment']); $comment = filter_var($comment, FILTER_SANITIZE_STRING); $comment = nl2br($comment); //this is where it would remove the new lines $comment = str_replace("\n", '', $comment); $comment = str_replace("\r", '', $comment); if($comment == "")> else
- 3 Contributors
- 9 Replies
- 1K Views
- 4 Hours Discussion Span
- Latest Post 10 Years Ago Latest Post by garyjohnson
Recommended Answers Collapse Answers
@garyjohnson
have tried every solution I have come across and am still not getting any results. I have a comment box and I am trying to remove all repetative new lines or /r/n from the input.
You need to add Trim() function to do that.
Try using a regular expression to replace multiple instances of CRLF and newline with a single instance. You probably also want to consider including break tags:
// Squeeze break tags $comment = preg_replace('/()+/', '
', $comment); // Squeeze CRLF $comment = preg_replace('/(\r\n)+/', '\r\n', $comment); // Squeeze NL …
My bad, I should have used double quoted strings:
$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);
All 9 Replies
have tried every solution I have come across and am still not getting any results. I have a comment box and I am trying to remove all repetative new lines or /r/n from the input.
$comment = trim(preg_replace('/[\n\r]/', ' ', $comment));
Try using a regular expression to replace multiple instances of CRLF and newline with a single instance. You probably also want to consider including break tags:
// Squeeze break tags $comment = preg_replace('/()+/', '
', $comment); // Squeeze CRLF $comment = preg_replace('/(\r\n)+/', '\r\n', $comment); // Squeeze NL $comment = preg_replace('/(\n)+/', '\n', $comment);