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

Member Avatar for diafol

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

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

Member Avatar for diafol

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

Member Avatar for diafol

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;
?>

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

@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

Member Avatar for diafol

@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.

@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. ;)

Member Avatar for diafol

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.

Oh, wow, @almostbob. I never noticed that.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.