Friday, July 22, 2022

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

Issue

I know REGEX well enough but am stuck on this one. I am using PHP 5.4 to shell_exec() a grep statement with REGEX inside it to capture the SKU in a text file. In php I don't see an error but the grep doesn't work.

The goal is to capture the SKU in this text:

{'Product_Product_SKU':               'DOP36M94DPM'},

PHP Code:

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

Furthermore, when I look at the $cmdSKU1 variable in the debugger tool of the IDE I am using it's value is this as one line:

grep -Po '(?<=\{'Product_Product_SKU':\s{15}').+?(?='\}\,)' /opt/web/default/RetailerParsing/RetailerDownload/MDD30TMSTH__20220113091043.htm

Furthermore some more, when I paste it into the Ubuntu console it gives me the error:

-bash: syntax eror near unexpected token `)'

Even if I go back and escape the exposed single quotes. Infact, I escaped everything [^A-Za-z0-9] just to make sure! No beans.

Lastly, if I try to double escape the single quotes within the REGEX in the PHP IDE I get a red squiggly underline error under the words Produce_Product_SKU.


Solution

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.



Answered By - Fravadona
Answer Checked By - Clifford M. (WPSolving Volunteer)