Forum Moderators: coopster
<?php
// include trailing slash in file
$url_to_grab = "http://www.amazon.com";
//IMPORTANT: BOTH OF THESE MUST BE UNIQUE PIECES OF CODE FROM
//THE TARGET WEBSITE--IF BOTH OF THESE ARE NOT UNIQUE,
//THIS SCRIPT WILL NOT WORK!
$start="List Price:</td>";
$end="";
// Grab source code from a file or web site
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}
while(!feof($myFile)) {
// Read each line and add to $myLine
$myLine.=fgets($myFile,255);
}
fclose($myFile);
$start_position=strpos($myLine, $start);
$end_position=strpos($myLine, $end)+strlen($end);
$length=$end_position-$start_position;
$myContent=substr($myLine, $start_position, $length);
echo $myContent;
?>
What is your goal, here? Are you hoping to scour am****.com for list prices and return them? Could you post a tiny bit of code that illustrates the section of the file you are attempting to read and the data you want to receive?
i know how to get the start location
$start=>List Price:</td>
but cannnot figure out the end. Already tried "<td>" and various other combinations, the thing is the end variable has to be unique, so i was thinking why not just take the next few letter that come after "List Price:</td>"
The Script Source is something like this
<td class="productLabel">List Price:</td>
<td>$30.00</td>
[edited by: jatar_k at 7:34 pm (utc) on Dec. 27, 2004]
[edit reason] fixed sidescroll [/edit]
$start_position=strpos($myLine, $start)+strlen($start);
$substring=substr($myline, $start_position);
$end_position=strpos($substring, $end);
$myContent=substr($myLine, 0, $end_position);
echo $myContent;
---------
$start will be Like you had it. And $end can now be '</td>' or whatever.
The change will make a new string starting from the position where $start ended. And will search from that point onwards for whatever you have in $end. It will then extract the string up to in front of that point.
So on the page you gave as an example, it'll extract '$30.00'
No, I didn't test it. ;-)
-V
<?php
$url_to_grab = "http://www.amazon.com/exec/obidos/tg/detail/-/0534492584/";
$start="List Price:</td>";
$end="</td>";
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}
while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);
$start_position=strpos($myLine, $start)+strlen($start);
$substring=substr($myline, $start_position);
$end_position=strpos($substring, $end);
$myContent=substr($myLine, 0, $end_position);
echo $myContent;
?>
[edited by: jatar_k at 7:35 pm (utc) on Dec. 27, 2004]
[edit reason] shortened url [/edit]
<?php
$url_to_grab = "[some ama***.com query, as above]";
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}
while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);
# THIS CODE DIRECTLY PRECEDES THE FIRST LIST PRICE DISPLAY
# I ADDED 18 TO $start_position TO KEEP THE VARIABLE FROM DISPLAYING, TOO
# CHANGE TO class=\"price\" AND + 14 TO GRAB THE FIRST CURRENT PRICE
$start_position=strpos($myLine, "class=\"listprice\">") + 18;
# WHY MESS AROUND? END_POSITION IS MAX 15 CHARS FROM START, RIGHT?
# HECK, MAKE IT 40 FOR THE REALLY EXPENSIVE BOOKS!
# SEPARATE THE RESULT INTO AN ARRAY, SPLIT AT THE FIRST "<" CHAR
$myContentA=explode("<",substr($myLine, $start_position, 15));
# THE FIRST ELEMENT IN THE ARRAY IS THE FIRST LIST PRICE
# THE REST OF THE ARRAY IS EVERYTHING AFTER THE FIRST "<" CHAR
# WE DON"T NEED THAT, SO JUST GRAB THE FIRST ELEMENT
$myContent=$myContentA[0];
# BINGO.
echo $myContent;
?>
the source for the used book is like this
<span class="tiny">from</span>
<span class="price">$49.50</span>
however currenlty the script goes further down the page and pulls out a value of 141.70
the source of that is below
<span class="price">$141.07</span>
<?php
$url_to_grab = "http://www.ama***.com/exec/obidos/ASIN/0534492584/qid=1103837824/sr=2-1/ref=pd_ka_b_2_1/002-9953569-5954449";
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}
while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);
$myLine2=$myLine;
# THIS CODE DIRECTLY PRECEDES THE FIRST LIST PRICE DISPLAY
# I ADDED 18 TO $start_position TO KEEP THE VARIABLE FROM DISPLAYING, TOO
# CHANGE TO class=\"price\" AND + 14 TO GRAB THE FIRST CURRENT PRICE
$start_position=strpos($myLine, "class=\"price\">") + 14;
$start_usedposition=strpos($myLine2, "span class=\"price\">");
# WHY MESS AROUND? END_POSITION IS MAX 15 CHARS FROM START, RIGHT?
# HECK, MAKE IT 40 FOR THE REALLY EXPENSIVE BOOKS!
# SEPARATE THE RESULT INTO AN ARRAY, SPLIT AT THE FIRST "<" CHAR
$myContentA=explode("<",substr($myLine, $start_position, 40));
$myContentB=explode("<",substr($myLine2, $start_usedposition, 40));
# THE FIRST ELEMENT IN THE ARRAY IS THE FIRST LIST PRICE
# THE REST OF THE ARRAY IS EVERYTHING AFTER THE FIRST "<" CHAR
# WE DON"T NEED THAT, SO JUST GRAB THE FIRST ELEMENT
$myContent=$myContentA[0];
$myContentused=$myContentB[0];
# BINGO.
echo $myContent;
echo $myContentused;
?>
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}
while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);
$pricecheck=strpos($myLine, "class=\"price\">")+ 14;
if($pricecheck===false){
$pricecheck=strpos($myLine, ">Availability:<");
}
$red=substr($myLine, $pricecheck, 1200);
$myContentA=explode("<",$red);
$myContent=$myContentA[0];
echo "Amazon (New): US".$myContent;
$red2=substr($red, strpos($red, "new<")+62);
$newvar=explode("<",$red2);
$usedprice=$newvar[0];
echo "Amazon (Used): US ".$usedprice;
echo"<br/>";
?>