Life, the Internet, and Everything!

Cool preg_replace Solution

The circumstance: Read an item description from an item database.
The issue: The block of text for the item description in the comma delimited file is not formatted, and in fact looks stripped of whatever formatting it had previously. Portions of text squished together like “changes.Special” (no area after the duration), and “Disc BrakeHeadset” (certainly missing a space in between Brake and Headset).
I surmised that the missing areas were previously line breaks ( n or << br>>). If a scan/replace function had actually been utilized and the line break characters changed with a null worth, it would fit the puzzle completely.
My very first idea was to break the string up into a big variety of specific characters, gone through the selection, and inspect the current character against the next character and the previous character to identify if there should be a brand-new area or line break. This is where my mind went based upon past experience with comparable scenarios while coding in RPG. It needs to work great for punctuation, but the other things – well see.
I established the following as a PHP function
$ array = str_split($ string);.
$ fixed = variety();.
$ punc = variety(., :, ));.
foreach ($ selection as $crucial => > $worth)

PHP String Functions( programmingquery.wordpress.com).

My very first thought was to break the string up into a big variety of specific characters, checked out through the array, and inspect the existing character against the previous character and the next character to determine if there ought to be a brand-new area or line break. The line breaks made sense – however there was a bunch of text at the bottom of the description that the routine did not fix. I had to figure out a way to break the text when a character bumped up against another character.
The very first operation looks for the punctuation isolated between a lower case character and an upper case character, and inserts the breaks between the punctuation ($ 2) and the upper case character ($ 3). The operation looks for “nothing” in between a lower case and upper case character, and insert the breaks in that null space.

Routine Expression Quick Look-Up( geekgumbo.com).

Value of string from preg_match( stackoverflow.com).

It worked fantastic – fun punctuation. The line breaks made sense – however there was a lot of text at the bottom of the description that the regimen did not fix. I had to find out a method to break the text when a character bumped up against another character.
Go into preg_replace.
After googling numerous terms, I lastly discovered a hint in a forum post. The example did not work, but it lead me in the right instructions. The last service appeared like this:.
Heres how it works. The first operation looks for the punctuation isolated between a lower case character and an upper case character, and inserts the breaks in between the punctuation ($ 2) and the upper case character ($ 3). The operation tries to find “nothing” in between a lower case and upper case character, and insert the breaks in that null space.
It works like a charm. At this point it handles most of the scenarios in this specific case. Others may arise, but Im thinking preg_replace will handle those also.
Pretty cool.
Associated short articles.

Tags: , , , , , , , , , , ,
Previous Post
The Walking Dead

The Walking Dead: Merle, We Hardly Knew Ya

Next Post
The Walking Dead

The Walking Dead: The “Andrea”sode

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: