2

Anyone know why this is not returning a value?

Example $item2['data'] where it = UPC
//===================================================//
Author:  Bob Smith  Orig. Published:  December 12, 1980
Format:  Softcover  UPC:  5960605543-04811 Price:  $6.99

//======================================================//
Example $item2['data'] where it = ISBN
//======================================================//
Author:  Jane Smith  Orig. Published:  December 1, 1985
Format:  Hardcover  ISBN #: 978-0-7851-5773-1 Price:  $26.99

//======================================================//
The Code
//======================================================//

$find_stockcode = $item2['data'];

$pos = strpos($find_stockcode, "ISBN");

if ($pos === false) 
{
    $pos = strpos ($find_stockcode, "UPC");
    if($pos === false) 
    {
        $arr = str_split('ABCDEFGHIJKLMNOP0123456789'); 
        shuffle($arr);
        $arr = array_slice($arr, 0, 16); 
        $str = implode('', $arr);
        $stock_num = $str;
    } else  {
          $stock_num = substr($pos, 5, 16);}
} else {
        $stock_num = substr($pos, 8,16); }

$upc = $stock_num;

if $find_stockcode returns UPC then $upc should be: 5960605543-04811

if $find_stockcode returns ISBN then $upc should be: 978-0-7851-5773-1

if $find_stockcode does not find UPC or ISBN then $upc should be a random 16 alpha-numeric string.

hakre
  • 193,403
  • 52
  • 435
  • 836
Justin Lucas
  • 319
  • 2
  • 18

2 Answers2

0

I believe the best approach for what you're trying to do here is a regular expression.

For your specific task, this should do it:

$number = preg_match("/((UPC)|(ISBN))[^0-9-]+([0-9-]+)/", $input, $fields);

if (count($fields) > 1) {
    $upc = $fields[4];
} else {
    $arr = str_split('ABCDEFGHIJKLMNOP0123456789'); 
    shuffle($arr);
    $arr = array_slice($arr, 0, 16); 
    $upc = implode('', $arr);
}

If you want to know which number code was found, use $fields[1].

Good luck

Robson França
  • 661
  • 8
  • 15
  • This causes strange errors with the rest of the script. It does return the UPC values, but it hangs the script. I had to kill the process to view the results. Oddly, it created only 3 products out of 100+ in 5 minutes time and duplicated them 3 times. And in one of the products it put the SKU in the price variable. Anyway to use the above that I have and figure out why it's not capturing the data? The script was taking 10 seconds or so to process the 100+ products. – Justin Lucas Apr 01 '12 at 04:34
  • That's odd. I need the remaining code to understand why this is happening – Robson França Apr 01 '12 at 19:38
  • 1
    It turned out not to be the code above. It was the code above that just tipped the script over the edge. The data listed above was exploded and I had done like 20+ variables and other crazy things, I deleted it all and re-wrote each one based upon preg_match rather than explode and selecting the arrays - which turned out to vary on some products anyhow. the preg_match works perfectly now. :) – Justin Lucas Apr 01 '12 at 20:18
0

The only reason way, this could happen is, if $pos does not have enough characters in it and substr returns false or "". Are you sure $pos contains enough characters if it has "UPC" or "ISBN" in it?

mani
  • 55
  • 4