How to resolve unexpected token error with grep, regex, and PHP?

Solution 1:

You're not escaping the shell command correctly. Instead of doing it manually, you should use ​escapeshellarg:

$cmdSKU1 = join(" ", array_map( 'escapeshellarg', array(
   ​"grep",
   ​"-Po",
   ​"(?<={'Product_Product_SKU':\s{15}').+?(?='},)",
   ​$FileName
)));
$chkSKU1 = trim(shell_exec($cmdSKU1));

That said, why not directly use preg_match and save a call to a non POSIX external command?

$string = "{'Product_Product_SKU':               'DOP36M94DPM'},";

preg_match("/{'Product_Product_SKU':\s{15}'([^']+)'}/", $string, $matches);
var_dump($matches[1]);
string(11) "DOP36M94DPM"

Of course you'll have to read the file inside php, but it may be worth the extra code.