如何从数组中使用php在javascript中使用正则表达式特殊字符

I have an array that also contains regex special characters in its some values, I want to implode() it so that special characters in its some values escape using preg_quote()

Here is what I tried

$arr = array("+1", "1+4"); 
echo implode("|", $arr);

I want escaped output like this

\+1|1\+4|

You could use array_map() with preg_quote() like this :

$arr = array("+1", "1+4"); 
echo implode("|", array_map('preg_quote', $arr));

Outputs :

\+1|1\+4

To get the final pipe :

$arr = array("+1", "1+4" , ""); 
echo implode("|", array_map('preg_quote', $arr)) ;
// Or
$arr = array("+1", "1+4"); 
echo implode("|", array_map('preg_quote', $arr)) . "|" ;

Outputs :

\+1|1\+4|