Re: how to strip empty lines out of a txt using preg_replace()
| From: | Sam Stelfox | Date: | Fri, 04 Sep 2009 14:08:06 +0000 |
| Subject: | Re: how to strip empty lines out of a txt using preg_replace() | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297676@lists.php.net to get a copy of this message | ||
The following snippet is untested and using Ash's regex (it is accurate \s matches any white space). $content is what is getting stripped of the new lines and $filtered is the cleansed output. See if that does the trick for you.
$lines = str_split(PHP_EOL, $content);
$filtered = '';
foreach ($lines as $line) {
if (!preg_match('^\s*$', $line)) {
// Splitting on the PHP_EOL characters cause it to be removed be sure to put it back
$filtered .= $line . PHP_EOL;
}
}
Ralph Deffke wrote:
the problem is some have got \t\n some are just \n\n....\n using PHP_EOL is a must I thing must be something with the /../sm attributes to the regex, spend like half an hour, but didn't get it, I'm running against a dead line, doesn't seem to be that easy if regex is not the everydays need u have "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message news:1252071327.24700.152.camel@localhost...lookOn Fri, 2009-09-04 at 15:28 +0200, Ralph Deffke wrote:The regex that would match a line containing only whitespace wouldpreg_replace( "/^\s*$/m", "", $somestring) does not take empty lines out "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote in message news:1252069539.24700.150.camel@localhost...okthanks ralph_deffke@yahoo.deOn Fri, 2009-09-04 at 14:58 +0200, Ralph Deffke wrote:snap.Hi all, I'm a bit under stress, maybe somebody knows the regex on ausing PHP_EOL would be great.
Are the lines actually whitespace, or are they actually <br/> tags that are inserting lines to format the page for HTML display? Thanks, Ash http://www.ashleysheridan.co.uklike this: ^\s*$ Thanks, Ash http://www.ashleysheridan.co.uk