1

I am hoping someone can help me. I have a variable in PHP that I need to use in a bash script. That string in the variable needs to be transformed to a specific format to wrap every word in the string in double quote and backslash.

Here is it specifically:

$raw = "amazon,facebook,reddit";

The output of the desired function needs to look like this:

\"amazon\",\"facebook\",\"reddit\"

Obviously the $raw variable could hold more or less words separated by comma.

The best I could do using str_replace is this:

$trfm = '"' . str_replace(",", "\",\"", $raw) . '"';

echo $trfm;

"amazon","facebook","reddit" 

so I need to figure out how to add the backslashes.

Thanks

2 Answers 2

2

You could use array_walk() for this.

// Input
$raw = "amazon,facebook,reddit";

// Turn items into an array, separated by commas
$items = explode(",", $raw); // ['amazon', 'facebook', 'reddit']

// Walk over each item in the array, and transform it
array_walk($items, function (&$value) {
  $value = '\"' . $value . '\"'; 
});

// Output: \"amazon\",\"facebook\",\"reddit\"
echo implode(",", $items);

For completeness, if you didn't want to split this into an array, and wanted to go with your approach of swapping commas with the backslashes, you could do the following. Note the use of double slashes, which are used by double quotes, to avoid having to escape these, you could use single quotes instead.

// Input
$raw = "amazon,facebook,reddit";

// Notice the double backslash below: \\
echo "\\\"" . str_replace(",", "\\\",\"", $raw) . "\\\"";

// Notice the single quotes and the lack of escaping with double backslash:
echo '\"' . str_replace(",", '\",\"', $raw) . '\"';
4
  • better solution than mine I have to admit Apr 16, 2021 at 22:53
  • just a short question I have never heard of array_walk is this pretty much the same as using a foreach loop ? and why do you have to write &$value? what does the & in that case mean? Apr 16, 2021 at 23:00
  • 1
    Slightly similar to a foreach loop, yes. array_walk() iterates over each item within the passed array (argument 1). The usage of the & inside of the callback argument for $value is to make use of PHP references. This allows us to update the array's item value by just updating the $value variable, instead of having to expose the key inside of the callback and use something like $items[$k] = "my new value"; - more information is on the PHP docs for array_walk.
    – Reece
    Apr 16, 2021 at 23:04
  • Sir, you are a life saver! Not only did it work for the problem I posted about.. but the thorough solution helped solved another code issue I almost gave up on. I cannot thank you enough.
    – bnks
    Apr 16, 2021 at 23:21
1

Hi this would be my solution:

    $raw = "amazon,facebook,reddit";
    $raw_arr = explode(',',$raw); 

    $output = ''; 

    for($i = 0; $i < sizeof($raw_arr); $i++) 
    {

        $output = $output.'\"'.$raw_arr[$i].'\"';
        
        // Check if we need to add , --> don't add if it is last element
        if(! ($i == sizeof($raw_arr) -1) )
        {
            $output = $output.','; 
        }
 
    }

    echo $output;
1
  • 1
    This definitely works also. Thank you for addressing it.
    – bnks
    Apr 16, 2021 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.