FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CustomTextEscaping.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Fossology contributors
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
20 {
29  public static function escapeNewlines($value)
30  {
31  if ($value === null) {
32  return '';
33  }
34  $value = str_replace('\\', '\\\\', (string) $value);
35  return str_replace(array("\r\n", "\r", "\n"), '\\n', $value);
36  }
37 
44  public static function unescapeNewlines($value)
45  {
46  if ($value === null) {
47  return '';
48  }
49  return preg_replace_callback('/\\\\(.)/', function ($m) {
50  return $m[1] === 'n' ? "\n" : $m[1];
51  }, (string) $value);
52  }
53 }
Escape/unescape newlines for single-line CSV fields.
static unescapeNewlines($value)
Reverse escapeNewlines(): literal becomes a real newline, any other escaped character is unescaped ...
static escapeNewlines($value)
Flatten real newlines to a literal so a field stays on one physical CSV line. Backslashes are escap...
Utility functions for specific applications.