You are here: Re: remove all lines starting with b « PHP Programming Language « IT news, forums, messages
Re: remove all lines starting with b

Posted by yawnmoth on 11/09/08 11:43

yawnmoth wrote:
> Erwin Moller wrote:
> <snip>
>
> Thanks for the suggestion - that helped!
>
> I tried modifyng it such that it'd delete all lines not begining with a
> b but am once again having some difficulty...
>
> First, here's the (new) script:
>
> <?
> $contents = file_get_contents('dummy.txt');
> echo "\r\n";
> echo preg_replace('/^(?!b).*?\n/m','',$contents);
> ?>
>
> The (?!b) should, as I understand it, match everything but b. What
> instead seems to be happening is that, when using the dummy.txt file
> that was posted earlier, all lines begining with a are deleted, but the
> lines with c are still there.
>
> Now, I realize that in this example, I could just as effectively
> replace (?!b) with [^b] but this approach fails if b is anything other
> than a single character. (eg. if I wanted to replace b with
> 192.168.1.1, I couldn't use [^...] - I'd have to use something like
> (?!b))

Never mind - I got it working:

<?
$contents = file_get_contents('dummy.txt');
echo "\r\n";
echo preg_replace('/^(?!b).*?(?:\n|$)/m','',$contents);
?>

The problem was due to the fact that the last line - which in dummy.txt
didn't start with a b - didn't end with a new line character but rather
with a EOF character (or maybe not a character at all? is EOF actually
a character?)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация