PHP explode
Count logical lines in a saved note, including a blank line between two entries.
A simple example
explode
- Start with Two lines containing A then B
- Do this Read each line
- Result 2 lines
Syntax
count(explode("\n", $value))
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpCount handover tasks
A handover or shopping note stores one entry per line. The display needs a count of the entries after interpreting line boundaries rather than counting the entire note as one item.
<?php
$value = "Open\nCheck\nClose";
echo (count(explode("\n", $value))), PHP_EOL;
3
Each task occupies one line.
Preserve a blank row for an unsigned delivery
A line-based import may include a blank record or a last record without a terminating newline. Distinguish a legitimate line from the end of the input so the import keeps the right number of rows.
<?php
$value = "Arrived\n\nSigned";
echo (count(explode("\n", $value))), PHP_EOL;
3
The interior blank line still occupies its own row.
Read a studio checklist
A stage manager places each job on its own line. Count the lines of work in the saved note. The final line has no trailing newline.
What to watch for
A final newline convention and the recognized line endings vary by API. These examples use LF text without a final newline.