note 74545 modified in function.preg-replace by bjori
| From: | bjori@php.net | Date: | Tue, 17 Apr 2007 09:34:25 +0000 |
| Subject: | note 74545 modified in function.preg-replace by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125507@lists.php.net to get a copy of this message | ||
I thought I'd share this. It took me half a day to find the solution to this little problem,
regular expressions being somewhat hard to grasp if one is not used to them:
I had the situation where I wanted to reformat text entered into a forum or email with wrong newline
or line break characters. You know someone enters a long text, thinking he has to break the lines
himself so they do not get too long, break the width of existing windows or tables and create a
scrollbar.
So I had texts with too newline characters ("\n") which had to be removed BEFORE the text
could be (re-)wrapped automatically. However, paragraphs separated by two or more newline characters
("\n\n") should remain separated paragraphs. After much testing I came up with this
solution:
<?php
// $originalString = Your original, badly formatted string
// with too many unneeded line breaks.
// *** Regular Expression Syntax used:
// "\ " = escaped space character
// "\n" = newline character (=line break from Linux)
// "\r\n" = carriage return and newline character (=line break from Windows)
// "\s" = any whitespace character
// "\S" = any character that is not a whitespace character
// "+" = One or more of this character
// (?=...) = Positive Lookahead (ahead = AFTER current position)
// assertion (==).
// (?<=...) = Positive Lookbehind (behind = BEFORE current position)
// assertion (==).
// Select single newline preceded and followed by any
// non-whitespace character:
$patterns[0] = "/(?<=\S)\n(?=\S)/";
// Select single newline preceded by a non-whitespace character
// and one or more spaces, and followed by any non-whitespace
// character:
$patterns[1] = "/(?<=\S)\ +\n(?=\S)/";
// Do the same for Windows style line breaks:
$patterns[2] = "/(?<=\S)\r\n(?=\S)/";
$patterns[3] = "/(?<=\S)\ +\r\n(?=\S)/";
// Replace all the found single newline characters selected by
// $patterns with a single space:
$replacement = ' ';
// Let er rock:
$reformattedString = preg_replace($patterns, $replacement, $originalString);
// Show results:
echo '<span class="black12bold">Original Text:<br></span>';
echo '<span class="black12">';
// Show original string for HTML:
echo nl2br($originalString);
echo '<br><br></span>';
echo '<span class="black12bold">And here is the processed
text:<br></span>';
echo '<span class="black12">';
// Show reformatted string for HTML:
echo nl2br($reformattedString);
echo '<br><br></span>';
// PS: Now you can easily re-wrap the text in $reformattedString.
?>
Greetings,
Markie
--was--
Darn, my previous message apparently didn't
make it to the server although I got no error message.
O.K., here's the entire updated, corrected version.
Please delete any other messeges from me in this bord.
Thanks, Markie
-------------------
I thought I'd share this. It took me half a day to find the solution to this little problem,
regular expressions being somewhat hard to grasp if one is not used to them:
I had the situation where I wanted to reformat text entered into a forum or email with wrong newline
or line break characters. You know someone enters a long text, thinking he has to break the lines
himself so they do not get too long, break the width of existing windows or tables and create a
scrollbar.
So I had texts with too newline characters ("\n") which had to be removed BEFORE the text
could be (re-)wrapped automatically. However, paragraphs separated by two or more newline characters
("\n\n") should remain separated paragraphs. After much testing I came up with this
solution:
<?php
// $originalString = Your original, badly formatted string
// with too many unneeded line breaks.
// *** Regular Expression Syntax used:
// "\ " = escaped space character
// "\n" = newline character (=line break from Linux)
// "\r\n" = carriage return and newline character (=line break from Windows)
// "\s" = any whitespace character
// "\S" = any character that is not a whitespace character
// "+" = One or more of this character
// (?=...) = Positive Lookahead (ahead = AFTER current position)
// assertion (==).
// (?<=...) = Positive Lookbehind (behind = BEFORE current position)
// assertion (==).
// Select single newline preceded and followed by any
// non-whitespace character:
$patterns[0] = "/(?<=\S)\n(?=\S)/";
// Select single newline preceded by a non-whitespace character
// and one or more spaces, and followed by any non-whitespace
// character:
$patterns[1] = "/(?<=\S)\ +\n(?=\S)/";
// Do the same for Windows style line breaks:
$patterns[2] = "/(?<=\S)\r\n(?=\S)/";
$patterns[3] = "/(?<=\S)\ +\r\n(?=\S)/";
// Replace all the found single newline characters selected by
// $patterns with a single space:
$replacement = ' ';
// Let er rock:
$reformattedString = preg_replace($patterns, $replacement, $originalString);
// Show results:
echo '<span class="black12bold">Original Text:<br></span>';
echo '<span class="black12">';
// Show original string for HTML:
echo nl2br($originalString);
echo '<br><br></span>';
echo '<span class="black12bold">And here is the processed
text:<br></span>';
echo '<span class="black12">';
// Show reformatted string for HTML:
echo nl2br($reformattedString);
echo '<br><br></span>';
// PS: Now you can easily re-wrap the text in $reformattedString.
?>
Greetings,
Markie
http://php.net/manual/en/function.preg-replace.php