Regular expressions matching multi-lines

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Regular expressions matching multi-lines

Post by hannahd »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello, I have a text file with two lines.

Code: Select all

line1
line2
I want to match line1, but it doesn't find it even though I've used the /m modifier. I thought this allowed strings to apply to lines.

Is this because I am using Windows and that o/s uses \r\n for line breaks?

Code: Select all

$filename = "file.txt";

$fp = fopen($filename, "r") or die("Cannot open file");

$str = fread($fp, filesize($filename));

$pattern = "/line1$/m";

if(preg_match($pattern, $str, $matches)) {

print "matched: ";

for($i=0; $i<count($matches); $i++) {
print "{$matches[$i]}";
}
}
else {

print "no match";

}

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could just use [php_man]file[/php_man] or [php_man]fgets[/php_man]...
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

Thanks, I know I could do that, but the whole purpose is to learn regular expressions, and I wondered why it wouldn't work. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

'#^(.*)$#m'
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

feyd wrote:

Code: Select all

'#^(.*)$#m'
Thanks :)

I don't understand what that does, though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

match each line.
Post Reply