note 74545 added to function.preg-replace

From: Date: Mon, 16 Apr 2007 21:38:11 +0000
Subject: note 74545 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-125483@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 80.238.232.154 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/74545 Del: integrated -- https://master.php.net/note/delete/74545/integrated Del: useless -- https://master.php.net/note/delete/74545/useless Del: bad code -- https://master.php.net/note/delete/74545/bad+code Del: spam -- https://master.php.net/note/delete/74545/spam Del: non-english -- https://master.php.net/note/delete/74545/non-english Del: in docs -- https://master.php.net/note/delete/74545/in+docs Del: other reasons-- https://master.php.net/note/delete/74545 Reject -- https://master.php.net/note/reject/74545 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125483) next »