Forum Moderators: coopster

Message Too Old, No Replies

Reading a Remote File into a String and preg_matching

         

ukgimp

12:56 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the following little script. The file reads in fine but I cant figure out how to get the file into a string to strip the instances of the data that I need.

$remote = fopen($url, 'r');
$html = fread($remote, 1048576);
fclose($remote);

preg_match("/<font face=\"Tahoma,Arial,Helvetica\" size=2><b>(.*)<\/b><\/font>/i", $var, $name);

$name = $name[1];
echo $name;

The variables just come back blank.

However if I take a section of the html and manually insert it into a variable and comment out the \”s it works.

I must be missing something but I cannot for the life of me figure it out.

Cheers

Birdman

1:11 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I'm not sure where the error is occurring but, it seem like what you have should work.

It could have something to do with this warning:

Warning

When reading from network streams or pipes, such as those returned when reading remote files or from popen() and proc_open(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the example below.
fread() [php.net]

I also found this in the manual:

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.
file_get_contents() [php.net]

Regards,
Birdman

ukgimp

3:33 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cheers Birdman

Workout out it appears to be the fact that when I was testing I cut a small block of text to test locally. However that screwed my regex as the real version has line breaks and carriage returns. However when I remoev them with:

$var = ereg_replace("\n", "", $var);
$var = ereg_replace("\r", "", $var);

It somehow sucks the HTML code into the varible. Any ideas as what I am doing wrong.

Cheers

Birdman

4:33 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may want to try double escaping the quotes in the pattern.

preg_match("/<font face=\\"Tahoma,Arial,Helvetica\\" size=2><b>(.*)<\/b><\/font>/i", $var, $name);

Or if that doesn't work:

preg_match("/<font face=\\\\"Tahoma,Arial,Helvetica\\\\" size=2><b>(.*)<\/b><\/font>/i", $var, $name);

Hopefully it'll work for you.