0

I have a text file, with a lot of data in it. I've gotten as far as extracting field names between character statements.

What I would like my code to do is scan the ENTIRE text file. for some reason it stops with the first occurrences of the characters/string.

<?PHP

//First, open the file. Change your filename
$file = "datafile1.txt";
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));

for ($i=0; $i=100; $i+10){
    $word1='"cat_id';
    $word2='"category"';

    $a = strpos($contents, $word1);
    $b = strpos($contents, $word2);

    $between=substr($contents, $a, ($b - $a));

    echo $between;  

    //////////////////////////////////

    $word1='"category';
    $word2='"name"';

    $c = strpos($contents, $word1);
    $d = strpos($contents, $word2);

    $between=substr($contents, $c, ($d - $c));

    echo $between;  
    ////////////////////////////////////

    $word1='"name';
    $word2='"description"';

    $e = strpos($contents, $word1);
    $f = strpos($contents, $word2);

   $between=substr($contents, $e, ($f - $e));

   echo $between;  
}
fclose($handle);

?>

I got a return

"cat_id": "16349", "category": "Adventure", "name": "Assassin's Creed IV Black Flag - Xbox 360",

but it stops there, where there are repeated cat_id's and categories, and names of... well, computer games.

I need to scan the entire text file so the search repeats itself, hopefully get a list out of the output of the games and categories.

*edit: Sorry. here's a sample of the datafile that needs parsing.

"cat_id": "16349",
  "category": "Adventure",
  "name": "Assassin's Creed IV Black Flag - Xbox 360",
  "description": "It is 1715. Pirates rule the Caribbean and have es... (visit site URLs for full      description)",
  "updated_at": 1419672679,
  "width": "139.70",
  "sem3_id": "1AEIvknN7uwqG2GcwSCMK8",
  "created_at": 1374830955,
  "platform": "Xbox 360",
  "height": "12.70",
  "length": "190.50",
  "sitedetails": [
    {
      "sku": "B00BMFIXT2",
      "latestoffers": [
        {
          "seller": "JNJ Shop",
          "lastrecorded_at": 1419672600,
          "currency": "USD",
          "firstrecorded_at": 1419672600,
          "id": "7g2fpY7BOSE0sU2oKkUkeY",
          "price": "11.00",
          "shipping": "3.99",
          "condition": "New"
        },

200 lines later.....

"cat_id": "20923",
  "category": "Games",
  "name": "Disney Infinity Starter Pack - PlayStation 3",
  "description": "Product Description Platform: PlayStation 3 | Edit... (visit site URLs for full                            description)",
  "updated_at": 1419563879,
  "width": "269.24",
  "created_at": 1375817329,
  "sem3_id": "0FIqEyeRf4SMgiYaoKC6yO",
  "platform": "PlayStation 3",
  "height": "90.93",
  "length": "358.39",
  "sitedetails": [
    {
      "sku": "7635065",
      "latestoffers": [
        {
          "seller": "BestBuy",
          "lastrecorded_at": 1419552600,
          "firstrecorded_at": 1419015000,
          "currency": "USD",
          "availability": "In stock",
          "price": "66.98",
          "id": "5EefaVFIhs2UKYA0Q0qIae",
          "condition": "New"
        },
  • Are entries in your text file separated somehow (with new line, for example)? Could you copy/paste part of text file? – sinisake Dec 28 '14 at 09:53

1 Answers1

0

aIt doesn't actually stop. Each time you feed the same content there, and according to http://php.net/manual/en/function.strpos.php you should get the same occurrence of your specified text.

You might need to use 3rd parameter [, int $offset = 0 ], to point out where to start at next iteration. Smth. like:

$a = 0;
$b = 0;   

for ($i=0; $i=100; $i+10){
    $word1='"cat_id';
    $word2='"category"';

    $a = strpos($contents, $word1, $a);
    $b = strpos($contents, $word2, $b);

If you going to use the same words "cat_id", and "category", init them outside your iterations.

for catching all occurrences you'd better use "while" cycles:

$catWord = '"cat_id"';
$categoryWord = '"category"';

$a = 0;
$b = 0;
while (($a = strpos($content, $catWord, $a)) !== false) {
    $b = strpos($content, $categoryWord, $b);

    $between = ....
Arij
  • 100
  • 1
  • 6