note 75170 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 20:11:53 +0000 |
| Subject: | note 75170 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127725@lists.php.net to get a copy of this message | ||
Note Submitter: markschmid at NOSPAM dot gmx dot ch
----
Hi Admins,
Someone mailed me a thank you for my code snippet here in preg_replace() and that he might donate
something to the PHP project. So I thought I'd rewrite my entry, making it even better by
removing some typos and the unneeded CSS classes which I formerly forgot to remove. Please replace
my entire entry from 16-Apr-2007 11:38 with the following one, thanx:
----------------------
Reformatting text
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 too many
unneeded newline or line break characters. You know, someone enters a text, thinking he has to break
the lines himself while you'd rather have no new line characters and wrap the text with a PHP
function or an HTML table.
So I had texts with too many 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 as
// it might come from a user entry.
$originalString = "Humpty Dumpty \r\n";
$originalString .= "sat on a wall \r\n";
$originalString .= "minding \r\n";
$originalString .= "his own business. \r\n";
$originalString .= "\r\n";
$originalString .= "After two hours\r\n";
$originalString .= "Puff the magic \r\n";
$originalString .= "dragon came along. \r\n\r\n";
// *** 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 assertion (sort of like ==)
// (ahead = AFTER current position).
// (?<=...) = Positive Lookbehind assertion (sort of like ==)
// (behind = BEFORE current position).
// With the first search pattern we select single newline
// characters preceded and followed by any non-whitespace
// character:
$searchPatterns[0] = "/(?<=\S)\n(?=\S)/";
// With the second search pattern we select single newline
// characters preceded by a non-whitespace character, then
// followed by one or more spaces as users might enter them
// and then followed by any non-whitespace character:
$searchPatterns[1] = "/(?<=\S)\ +\n(?=\S)/";
// Do the same for Windows style line breaks for texts
// entered from Windows computers:
$searchPatterns[2] = "/(?<=\S)\r\n(?=\S)/";
$searchPatterns[3] = "/(?<=\S)\ +\r\n(?=\S)/";
// Note: We don't have to use ksort($searchPatterns), as
// suggested in PHP documentation, because the order in
// which $searchPatterns are found is not relevant for us.
// Replace all the found $searchPatterns with $replacement,
// a single space:
$replacement = " ";
// Now let preg_replace() do the searching and replacing:
$reformattedString = preg_replace($searchPatterns, $replacement, $originalString);
// Show results:
echo 'Original Text:<br>';
// Show original string for HTML:
echo nl2br($originalString);
echo '<br><br>';
echo 'And here is the reformatted text:<br>';
// Show reformatted string for HTML:
echo nl2br($reformattedString);
echo '<br><br>';
// PS: Now you can easily re-wrap the text in $reformattedString
// with a PHP function of HTML tables.
?>
Greetings,
Markie