0

Hello,

I have a webpage which has the follwing source code:

<div class=prod_text>
<b>Category:</b> <a href='/Games/Xbox-360/Action_Adventure/'>Action Adventure</a><br><br><b>Publisher:</b> <a href='/stores/Ubisoft/'>Ubisoft (click here for our Ubisoft store)</a><br><br><b>Barcode (EAN) No:</b> 3307215794227<br><b>Barcode (EAN2) No:</b> 3307215794234<br><b>Barcode (EAN3) No:</b> 3307215794241<br><b>Barcode (EAN4) No:</b> 3307215801895<br><br><i class='psm'><b>Please Note:</b> Any images are used for illustrative purposes only, actual products and/or packaging may differ<br><br></i>
</div>

With the below code I want to obtain the products category which is Action Adventure - however cannot seem to get it to work.

<?php

$site=file_get_contents("http://www.gameseek.co.uk/pd/videogames2nbjhuxhsb/xbox-360-far-cry-4-limited-edition");
$price='#<b>Category:</b> (.*?)#';
preg_match_all($price,$site,$pricelist);

print_r($pricelist);

?>

Am I missing something?

Kind regards,

Jack

Edited by Jackyjack21

4
Contributors
11
Replies
42
Views
2 Years
Discussion Span
Last Post by almostbob
Featured Replies
  • 1
    diafol 3,720   2 Years Ago

    You may find using an xml parser or DOMDocument with xpath easier. Read More

  • 1
    diafol 3,720   2 Years Ago

    Placed your html into xpath.html file and did this: <?php $file = "xpath.html"; $doc = new DOMDocument(); $doc->loadHTMLFile($file); $xpath = new DOMXpath($doc); $elements = $xpath->query("*/div[@class='prod_text']/a"); $p = "Couldn't find the category name"; if (!is_null($elements)) { $p = $elements->item(0)->childNodes->item(0)->nodeValue; } echo $p; ?> Read More

  • > @diafol — How come you put in those closing PHP tags? Just curious. It's PHP only. ;D prior versions of php have insisted on closures, EOF does not close the php script. currently the manual says [If a file is pure PHP code, it is preferable to omit the … Read More

  • 1
    diafol 3,720   2 Years Ago

    @matrix While the snippet may be pure php. It's not to say that the code used in the OP's file will be pure php. I always (when I remember) place the closing tag - just in case - not for my benefit, but for theirs. Read More

1

You may find using an xml parser or DOMDocument with xpath easier.

0

Thank you for your reply. Do you know where I can find some examples? I am quite new to PHP.

0

The php manual is full of great examples. If I have time, I'll post an example.

1

Placed your html into xpath.html file and did this:

<?php
$file = "xpath.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@class='prod_text']/a");
$p = "Couldn't find the category name";

if (!is_null($elements)) {
    $p =  $elements->item(0)->childNodes->item(0)->nodeValue;
}

echo $p;
?>
1

@diafol — How come you put in those closing PHP tags? Just curious. It's PHP only. ;D

prior versions of php have insisted on closures, EOF does not close the php script.
currently the manual says
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

not always good advice, , just because it is on the php site 'twas still written by people

if you have whitespace after the closing '?>' strange things happen
solution: don't screw up.

stranger things happen
If you include('file without ?>');

<doctype>
<html>
<head>
bla bla bla
</head><body>
bla bla bla
<?php include('file without ?>'); ?>
bla bla bla
</body></html>

expands to

<doctype>
<html>
<head>
bla bla bla
</head><body>
bla bla bla
<?php $file = "xpath.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("*/div[@class='prod_text']/a");
$p = "Couldn't find the category name";
if (!is_null($elements)) {
    $p =  $elements->item(0)->childNodes->item(0)->nodeValue;
}
echo $p;
bla bla bla
</body></html>

and fails dismally
solution: close script files & dont screw up

pure php, makes little or no difference;
close the script without extra whitespace

Edited by almostbob

1

@matrix

While the snippet may be pure php. It's not to say that the code used in the OP's file will be pure php. I always (when I remember) place the closing tag - just in case - not for my benefit, but for theirs.

0

@diafol
First, I almost typed @matrixdevuk. I'm so badass.
Second, I see why you'd do that for them; but they should have basic knowledge of PHP at least. ;)

0

Second, I see why you'd do that for them; but they should have basic knowledge of PHP at least. ;)

I concede your point, but you can't always assume that. almostbob makes some good observations.

Edited by diafol

0

For many people, asking here is the first step to getting basic knowlege.
when you google search, the answers on other sites are very often just links to DaniWeb, so I came to where the answers start
For me it was sql,
tutorials don't always make sense
I call them 'headslap moments' when the answer is aarrgghh simple after reading replies to my own questions

Edited by almostbob

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.