php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nino at recgr dot com
New email:
PHP Version: OS:

 

 [2006-09-04 19:43 UTC] nino at recgr dot com
Description:
------------
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('http://www.example.com'), $found);

Expected result:
----------------
Found matches in array $found.

Actual result:
--------------
Nothing.
However, when put your script thus:

$url = file_get_contents('http://www.example.com');

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

then it works.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-04 20:19 UTC] tony2001@php.net
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
WORKING VERSION:

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

NON-WORKING VERSION:

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] tony2001@php.net
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] tony2001@php.net
Private reproduce code?

Works perfectly fine here.

<?php
preg_match("/(.*)/", file_get_contents('http://www.google.com'), $found);
var_dump($found);
?>

array(2) {
  [0]=>
  string(118) "<html><head><meta http-equiv="content-type" content="text/html; charset=windows-1251"><title>Google</title><style><!--"
  [1]=>
  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-2025 The PHP Group
All rights reserved.
Last updated: Tue Mar 11 06:01:29 2025 UTC