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
hannahd
Forum Newbie
Posts: 16 Joined: Wed Feb 18, 2004 9:40 am
Post
by hannahd » Sun Sep 12, 2004 12:26 pm
feyd | Please use 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.
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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 12, 2004 12:40 pm
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 » Sun Sep 12, 2004 1:23 pm
Thanks, I know I could do that, but the whole purpose is to learn regular expressions, and I wondered why it wouldn't work.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 12, 2004 1:39 pm
hannahd
Forum Newbie
Posts: 16 Joined: Wed Feb 18, 2004 9:40 am
Post
by hannahd » Sun Sep 12, 2004 2:16 pm
Thanks
I don't understand what that does, though.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Sep 12, 2004 2:34 pm
match each line.