【问题标题】:PHP add whitespaces in string with an URLPHP在带有URL的字符串中添加空格
【发布时间】:2021-01-09 11:30:11
【问题描述】:

问题

我有一个临时 url 可以从 AWS-S3 访问对象

$source = "http://mybucket...";

如果我执行 var_dump($source),它会显示正确的 URL

string(407) "https://mybucket.s3.amazonaws.com/storage/..."

所以我尝试以这种方式在 ffmpeg 命令中使用此 url(ffmpeg 中需要双引号)

$cmd = 'ffmpeg -i "' . $source . '" ...
shell_exec($cmd);

这不起作用,当我执行 var_dump($cmd) 时,网址显示如下(https:// 之间的空格)

 "https: //mybucket.s3.amazonaws.com/storage/..."

当我使用 S3 协议指定 url 时它工作正常

"s3://mybucket...."

所以问题可能出在 https 协议上

我试过的(没用)

  • 我试过$url = str_replace(' ', '', $source);
  • 我试过$url = str_replace('%20', '', $source);
  • 我试过$url = str_replace('https: //', 'https://', $source);
  • 我试过$url = preg_replace("/\s+/", "", $source);
  • 我尝试了addslashes($source) 而不是'"' . $source . '"'
  • 我试过$url = escapeshellarg($source);

我正在使用什么

  • PHP 7
  • Laravel 4
  • AWS-S3

【问题讨论】:

  • $cmd= str_replace(': // ', '://', $cmd); 怎么样?我看不出为什么会突然出现这个空间
  • $url = str_replace(' ', '', $source); 的输出结果是什么???
  • 我也尝试过这种方式@AntG,我得到了相同的结果。
  • 我有相同的结果@sta,带有空格的字符串 `"mybucket.s3.amazonaws.com/storage..."´
  • 上述方法应该可以工作,如果它只是一个空格。我想你得到了https:%20//,如果是这样,那么你可以试试$url = str_replace('%20', '', $source);

标签: php laravel string amazon-s3 ffmpeg


【解决方案1】:

你为什么不使用sprintf?! 我尝试过并且像冠军一样工作!

$source = "https://mybucket.s3.amazonaws.com/storage/...";
$cmd = sprintf('ffmpeg -i "%s"', $source);

$array = [
    'source' => $source,
    'cmd' => $cmd
];

echo '<pre>'; print_r($array); echo '</pre>';
echo '<pre>'; var_dump($array); echo '</pre>';

结果是:

Array
(
    [source] => https://mybucket.s3.amazonaws.com/storage/...
    [cmd] => ffmpeg -i "https://mybucket.s3.amazonaws.com/storage/..."
)
array(2) {
  ["source"]=>
  string(45) "https://mybucket.s3.amazonaws.com/storage/..."
  ["cmd"]=>
  string(57) "ffmpeg -i "https://mybucket.s3.amazonaws.com/storage/...""
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多