kid85 Posted December 29, 2007 Share Posted December 29, 2007 I'm using PHP5.2.4 and don't know which of the 2 methods is better or more standard. Any other suggestions would be great. I just need to open a file which is inside my server and search for 1 string using preg_match regex. I would need to read all of its contents no need to write anything. $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); // do regex stuff fclose($handle); Quote Link to comment https://forums.phpfreaks.com/topic/83645-solved-fread-or-file_get_contents/ Share on other sites More sharing options...
tibberous Posted December 29, 2007 Share Posted December 29, 2007 file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance. Quote Link to comment https://forums.phpfreaks.com/topic/83645-solved-fread-or-file_get_contents/#findComment-425510 Share on other sites More sharing options...
kid85 Posted December 30, 2007 Author Share Posted December 30, 2007 The file I need to read is not a database its just a normal web page. fread and preg_match could be discarded in favor of fscanf which combines both functions... is that correct ? Any idea of which functions would be faster ? Quote Link to comment https://forums.phpfreaks.com/topic/83645-solved-fread-or-file_get_contents/#findComment-425570 Share on other sites More sharing options...
trq Posted December 30, 2007 Share Posted December 30, 2007 fread and preg_match could be discarded in favor of fscanf which combines both functions... is that correct ? Not at all. file_get_contents and preg_match are probably your best options. Quote Link to comment https://forums.phpfreaks.com/topic/83645-solved-fread-or-file_get_contents/#findComment-425580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.