我有一个字符串数组,每个字符串包含一个图像文件的名称:
$slike=(1.jpg,253455.jpg,32.jpg,477.jpg);
Run Code Online (Sandbox Code Playgroud)
我想要新的数组,看起来像这样:
$slike=(1,253455,32,477);
Run Code Online (Sandbox Code Playgroud)
如何从此数组中的每个字符串中删除文件扩展名?
如果您正在使用文件名,请使用PHP内置的pathinfo()函数.没有必要为此使用正则表达式.
<?php
# your array
$slike = array('1.jpg','253455.jpg','32.jpg','477.jpg');
# if you have PHP >= 5.3, I'd use this
$slike = array_map(function($e){
return pathinfo($e, PATHINFO_FILENAME);
}, $slike);
# if you have PHP <= 5.2, use this
$slike = array_map('pathinfo', $slike, array_fill(
0, count($slike), PATHINFO_FILENAME
));
# dump
print_r($slike);
Run Code Online (Sandbox Code Playgroud)
产量
Array
(
[0] => 1
[1] => 253455
[2] => 32
[3] => 477
)
Run Code Online (Sandbox Code Playgroud)