How to Detect & Change the Line Endings of a Text File in PHP

Cloudmersive
3 min readJun 10, 2024

--

Using a few lines of code, we can easily check which line endings our text files are using — and we can just as easily change those line endings to another OS format.

Specifically, we’ll call a pair of free APIs to automate this process for us. Our first API call will identify the line endings in a text file, and the second API call will let us modify the line endings in our file with a simple text input value.

Example

Let’s look at a quick example. Say we start with a text file containing Lorem Ipsum passage. This file was created on Windows notepad, so our first API call will return the following response:

{
"Successful": true,
"PrimaryNewlineType": "Windows",
"PrimaryNewlineTerminator": "\r\n",
"InputLength": 457
}

Now that we know the Newline Type is Windows (and our Newline Terminators follow Windows format), we can use our subsequent API call to change the line endings to “Unix”. We’ll see the Newline Terminators \n\n in our below output instead of the \r\n detected in the original file:

{
"Successful": true,
"TextContentResult": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \n\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}

Just like that, we now have a text file set to “Unix” as the Newline Type.

Calling APIs with Code Examples

We can use the below ready-to-run PHP code examples to structure both of our API calls.

First, let’s use the following command to install the client SDK with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, let’s use the following code to call our line ending detection function:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\EditTextApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->editTextDetectLineEndings($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditTextApi->editTextDetectLineEndings: ', $e->getMessage(), PHP_EOL;
}
?>

And let’s use the following code examples to call our line ending change function:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\EditTextApi(


new GuzzleHttp\Client(),
$config
);
$line_ending_type = "line_ending_type_example"; // string | Required; 'Windows' will use carriage return and line feed, 'Unix' will use newline, and 'Mac' will use carriage return
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->editTextChangeLineEndings($line_ending_type, $input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditTextApi->editTextChangeLineEndings: ', $e->getMessage(), PHP_EOL;
}
?>

To authorize our API calls, we’ll just need a free Cloudmersive API key (this allows a limit of 800 API calls per month and no additional commitments). We can remove the ‘YOUR_API_KEY’ placeholder text and replace that with our own API key to finalize authorization.

No more code required — now we have an easy way to automate line detection & conversion in our PHP applications.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.