如何连接文件名和扩展名字符串?
How to concatenate filename and extension string?
我想创建带有“.xml”扩展名的文件名字符串。
我有 $fileName
变量,但我不知道如何将变量与“.xml”扩展名结合起来。
例如:从文件名 book1、book2、catalog1、catalog2,我想要以下输出:book1.xml、book2.xml、catalog1.xml、catalog2.xml。
在 PHP 中,您可以使用 .
连接字符串
因此,如果文件名位于名为 $filename
的变量中,则执行
$filename = 'a_file_name';
$filename_extn = $filename . '.xml';
或者要将扩展名添加到现有变量,您可以像这样使用 .=
连接符
$filename = 'a_file_name';
$filename .= '.xml';
如果文件名存储在 $fileName
变量中,您可以使用 string concatenation 将两个字符串连接在一起。
例如:
$fileName = "catalog1";
$extension = "xml";
// You can use the double quotes to build strings like this:
$fileNameWithExtension = "$fileName.$extension";
// Or you can concatenate using the "." operator:
$fileNameWithExtension = $fileName . "." . $extension;
// Of course, there are many ways to skin a cat:
$fileNameWithExtension = implode(".", [$fileName, $extension]);
$fileNameWithExtension = sprintf("%s.%s", $fileName, $extension);
我想创建带有“.xml”扩展名的文件名字符串。
我有 $fileName
变量,但我不知道如何将变量与“.xml”扩展名结合起来。
例如:从文件名 book1、book2、catalog1、catalog2,我想要以下输出:book1.xml、book2.xml、catalog1.xml、catalog2.xml。
在 PHP 中,您可以使用 .
因此,如果文件名位于名为 $filename
的变量中,则执行
$filename = 'a_file_name';
$filename_extn = $filename . '.xml';
或者要将扩展名添加到现有变量,您可以像这样使用 .=
连接符
$filename = 'a_file_name';
$filename .= '.xml';
如果文件名存储在 $fileName
变量中,您可以使用 string concatenation 将两个字符串连接在一起。
例如:
$fileName = "catalog1";
$extension = "xml";
// You can use the double quotes to build strings like this:
$fileNameWithExtension = "$fileName.$extension";
// Or you can concatenate using the "." operator:
$fileNameWithExtension = $fileName . "." . $extension;
// Of course, there are many ways to skin a cat:
$fileNameWithExtension = implode(".", [$fileName, $extension]);
$fileNameWithExtension = sprintf("%s.%s", $fileName, $extension);