简体   繁体   English

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

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

I am trying to work with an external array that should transform my file endings into another string: 我正在尝试使用应该将文件结尾转换为另一个字符串的外部数组:

This is my fileTypes.php : 这是我的fileTypes.php

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

And this is my index.php : 这是我的index.php

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

echo $fileTypes['fileExt'];

I do not really get it run. 我真的没有让它运行。 I must have some basic problem in understanding :( I get an empty result. But the result I wish is fa-music . 我在理解上必须有一些基本问题:(我得到的结果是空的。但是我希望结果是fa-music

Your fileTypes.php file would be like this: 您的fileTypes.php文件将如下所示:

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

Now, just include that file in your main script: 现在,只需将该文件包含在您的主脚本中:

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

echo $fileTypes[$fileExt];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM