public static function ProcessUtils::escapeArgument

Escapes a string to be used as a shell argument.

Parameters

string $argument The argument that will be escaped:

Return value

string The escaped argument

1 call to ProcessUtils::escapeArgument()
ProcessUtilsTest::testEscapeArgument in drupal/core/vendor/symfony/process/Symfony/Component/Process/Tests/ProcessUtilsTest.php
@dataProvider dataArguments

File

drupal/core/vendor/symfony/process/Symfony/Component/Process/ProcessUtils.php, line 37

Class

ProcessUtils
ProcessUtils is a bunch of utility methods.

Namespace

Symfony\Component\Process

Code

public static function escapeArgument($argument) {

  //Fix for PHP bug #43784 escapeshellarg removes % from given string

  //Fix for PHP bug #49446 escapeshellarg dosn`t work on windows

  //@see https://bugs.php.net/bug.php?id=43784

  //@see https://bugs.php.net/bug.php?id=49446
  if (defined('PHP_WINDOWS_VERSION_BUILD')) {
    $escapedArgument = '';
    foreach (preg_split('/([%"])/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
      if ('"' == $part) {
        $escapedArgument .= '\\"';
      }
      elseif ('%' == $part) {
        $escapedArgument .= '^%';
      }
      else {
        $escapedArgument .= escapeshellarg($part);
      }
    }
    return $escapedArgument;
  }
  return escapeshellarg($argument);
}