如何使用数组将一个字符串替换为另一个字符串


How can I replace a string with another string using an array?

我正在尝试使用一个外部数组,该数组应该将我的文件结尾转换为另一个字符串:

这是我的fileTypes.php:

return array(
'mp3'   => 'fa-music',
'jpeg'  => 'fa-picture-o',
);

这是我的index.php:

$fileTypes = require('fileTypes.php');
$file = "mymusic.mp3"
$fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
echo $fileTypes['fileExt'];

我并没有真正做到这一点。我一定有一些基本的理解问题:(我得到一个空结果。但我希望结果是fa-music

您的fileTypes.php文件如下所示:

$fileTypes = array(
  'mp3'   => 'fa-music',
  'jpeg'  => 'fa-picture-o',
);

现在,只需在主脚本中包含该文件:

require('fileTypes.php');
$file = "mymusic.mp3";
$fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
echo $fileTypes[$fileExt];