title: How to escape php exec() command with quotes
body: <p>I use the Exiv2 command line tool on Linux to edit image metadata like so:</p>
  
  <pre><code>exiv2 -M"set Iptc.Application2.Caption String This is my caption....." modify IMG.jpg
  </code></pre>
  
  <p>I want to execute this from PHP, using a caption provide by a user. This will work if the user enters no special characters:</p>
  
  <pre><code>exec('/usr/local/bin/exiv2 -M"set Iptc.Application2.Caption String '.$caption.'" modify IMG.jpg');
  </code></pre>
  
  <p>I need to allow the user special characters such as single and double quotes. I would like to use escapeshellcmd() to prevent malicious data. How can I correctly escape the command and the argument so that it works? I have tried many options but I can't get it right.</p>