Jump to content

[SOLVED] Problem with preg_match()


paradox28491

Recommended Posts

I'm trying to retrieve some text from a sites source file.

 

$url = "http://www.newegg.com/Product/Product.aspx?Item=N82E16813130221";
$handle = fopen($url, "r");
$contents = file_get_contents($url);
fclose($handle);


$data_pattern = '%<TD class="name">Brand</TD>
      <TD class="desc"><script type="text/javascript">document.write(neg_specification_newline('([A-Z])'));</script></TD>
%';

//I think the ' I'm using to wrap the variable in is getting messed up because there are some in the source code as well. Escaping them yields no match. Not escaping them gives me a parse error.

preg_match($data_pattern, $contents, $data);


while (list ($key, $val) = each ($data)) {
echo "$key -> $val <br>";
}

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/146206-solved-problem-with-preg_match/
Share on other sites

Ok well I think I've narrowed it down to something being wrong with my $data_pattern regex. Basically what I want to do is search for a certain bit of code in a websites source file and return a part of that code with preg_match.

 

The piece of code I am looking for is say:

 

<TD class="name">Brand</TD>
      <TD class="desc"><script type="text/javascript">document.write(neg_specification_newline('MSI'));</script></TD>

 

There is a lot of other code on this site, which is why I have such a large variable. I've been looking into writing regexes, but there are a couple things I am confused about. I think what I need is something like this:

 

/(any text)Brand(any text)
(any text)([A-Z])(any text)>/

 

The only part of that I want in the array is what I believe should be retrieved with ([A-Z]). I'm expecting to have one of the array's variables to be MSI, but I cannot get this result. Do I need to indicate that there are multiple lines? How do I indicate that there can be any text in those fields?

 

Am I right in leaving the > at the end so it knows when to stop? I would really appreciate some help with this as I've been trying to figure it out all day.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.