|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38715 preg_match fails if combined with file_get_contents directly!
Submitted: 2006-09-04 19:43 UTC Modified: 2006-09-05 20:05 UTC
From: nino at recgr dot com Assigned:
Status: Not a bug Package: Network related
PHP Version: 5.1.6 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: nino at recgr dot com
New email:
PHP Version: OS:


 [2006-09-04 19:43 UTC] nino at recgr dot com
When you put file_get_contents('some_file.ext') as a second argument in preg_match() it doesn't work. However, when you put that file_get_contents() into a variable, and THEN you insert that variable as a second argument, it works. At least at remote files (I'm not sure about local files) and when there is a third argument too.

Reproduce code:
preg_match("<pattern>", file_get_contents(''), $found);

Expected result:
Found matches in array $found.

Actual result:
However, when put your script thus:

$url = file_get_contents('');

preg_match("<pattern>", $url, $found);

then it works.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-04 20:19 UTC]
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

 [2006-09-05 17:49 UTC] nino at recgr dot com

function grabData($page) {
$lookHere = file_get_contents($page);
preg_match("<pattern>", $lookHere, $found);


function grabData($page) {
preg_match("<pattern>", file_get_contents($page), $found);

With that previous, non-working example, $found is an empty array, while the first example works.
 [2006-09-05 18:51 UTC]
Please provide REAL reproduce case without "<pattern>" and with exact URL, so we can copy/paste and reproduce the problem.
 [2006-09-05 19:55 UTC] nino at recgr dot com
I can't, it's private.

Well just try any pattern on any URL?
 [2006-09-05 20:05 UTC]
Private reproduce code?

Works perfectly fine here.

preg_match("/(.*)/", file_get_contents(''), $found);

array(2) {
  string(118) "<html><head><meta http-equiv="content-type" content="text/html; charset=windows-1251"><title>Google</title><style><!--"
  string(118) "<html><head><meta http-equiv="content-type" content="text/html; charset=windows-1251"><title>Google</title><style><!--"

Marked as bogus, since we don't have any way to reproduce it.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Aug 14 04:05:44 2022 UTC