Advertisement
Guest User

ASIN Number Extracting Example

a guest
Aug 4th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2.     $cur_file_path = realpath(dirname(__FILE__));
  3.  
  4.     // Set File Names
  5.     // Just place the input file in the same directory as this script
  6.     $input_file = $cur_file_path . "/list_of_input_urls.txt";
  7.     $output_file = $cur_file_path . "/list_of_output_urls.txt";
  8.  
  9.  
  10. /**
  11.     Unless you know what you're doing I wouldn't change anything below
  12. **/
  13.     $aBigAssArray = array();
  14.     $pattern = "/\/product\/([aA-zZ 0-9]*)\//";
  15.  
  16.     // Check if input file exists
  17. if ( file_exists($input_file) ) {
  18.     $input_handle = fopen($input_file, "r");
  19.     $output_handle = fopen($output_file, "w");
  20.    
  21.     if ($input_handle) {
  22.         while (($line = fgets($input_handle)) !== false) {
  23.             preg_match($pattern, $line, $aResults);
  24.             if ($aResults) {
  25.                 $asin = $aResults[1];
  26.                 if ( $asin ) {
  27.  
  28. /** Uncomment if you wanted to store all values in an array. */
  29. //                     array_push($aBigAssArray, $asin);
  30.  
  31.                     fwrite($output_handle, $asin . "\n");
  32.  
  33. /** Uncomment to view lines as they're being processed */
  34. // echo $asin . "</br>";
  35.                 }
  36.             }
  37.         }
  38.         fclose($input_handle);
  39.     } else {
  40.         // Error - Could not open file
  41.     }
  42.  
  43.  
  44. /** Uncomment if you wanted to view each line stored in the array. */
  45. //     foreach ($aBigAssArray as $asin_line):
  46. //         echo $asin_line . "</br>";
  47. //     endforeach;
  48.  
  49.     die('<hr>End Of Script');
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement