This really is a job better suited for regex than exploding and iterating.
$str = preg_replace('~\v+~', "\n\n", $str);
Here\v+
matches any number of vertical spaces, substituted with two\n
newlines. You will get a standard one line gap between your content (non-linebreak-only) lines. This results in:
Section 1
Section 2 (after 1 line break)
Section 3 (after 2 line breaks)
Section 4 (after 4 line breaks)
Section 5 (after 1 line break)
If you want to only target more than N breaks, use e.g.\v{4,}
, assuming EOL is\n
, to normalize all instances of 4 or more newlines. If your file uses Windows EOL (\r\n
), be aware that you need to double it up, or use(\r\n|\n){4,}
, since\r
and\n
each are one match of\v
.
That's the basic idea. Seeing as you want to replace 4+ newlines with horizontal space, merging the lines instead of normalizing line break quantity, you would simply:
$str = preg_replace('~(\r\n|\n){4,}~', " ", $str);
This would give you:
Section 1
Section 2 (after 1 line break)
Section 3 (after 2 line breaks) Section 4 (after 4 line breaks)
Section 5 (after 1 line break)
Here the gap with 4 or more EOLs was substituted with space and merged with the preceding line. The rest of the "acceptably spaced" lines are still in their places.
However it seems that you want to merge all subsequent rows into a single line after any gap of 4+ EOLs. Is that really the requirement? The first example I posted is a fairly standard operation for normalizing content with irregular linebreaks; especially "level items" like section headings!
OP: thanks for explaining your use case, makes sense. This can be regex-ed without loops:
$str = preg_replace_callback('~(\r\n|\n){4,}(?<therest>.+)~s', function($match) {
return ' ' . preg_replace('~\v+~', ' ', $match['therest']);
}, $str);
Here we capture (as named grouptherest
) all the content that follows four or more linebreaks usingpreg_replace_callback
, and inside the callback wepreg_replace
all vertical spaces in "the rest" with a single horizontal space. This results in:
Section 1
Section 2 (after 1 line break)
Section 3 (after 2 line breaks) Section 4 (after 4 line breaks) Section 5 (after 1 line break) Section 17 after a hundred bundred line breaks"
For convenience, here's the regex above wrapped in a function:
function fuse_breaks(string $str): string {
$str = preg_replace_callback('~(\r\n|\n){4,}(?<therest>.+)~s', function($match) {
return ' ' . preg_replace('~\v+~', ' ', $match['therest']);
}, $str);
return $str;
}
// Usage:
$fused_text = fuse_breaks($source_text);
Your example with N=3 shows either 4 line breaks – if the empty lines count –, or 2 line breaks.
To make things clearer this is a function limitedLines, which reduces the text to a specific amount of lines:
$str = "
line 1
line 2
line 3
line 4
line 5
line 6
";
function limitedLines(string $str = '', int $maxLines = 5): string {
$maxLines = $maxLines < 1 ? 1 : $maxLines;
$arr = explode("\n", trim($str));
while (count($arr) > $maxLines) {
$last = array_pop($arr);
$arr[array_key_last($arr)] .= ' ' . trim($last);
}
return implode("\n", $arr);
}
$result = limitedLines($str, 3);
print_r($result);
This will print:
line 1
line 2
line 3 line 4 line 5 line 6
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.