我正在寻找解决方案:
我有一条路径:
$PATH = images/large/Wildebeest-01.jpg
$PATH = images/large/greater-kudu-02.jpg
$PATH = images/large/BLUE-AND-YELLOW-MACAW-08.jpg
我需要的是:
"Wildebeest"
"Greater kudu"
"Blue and yellow macaw"
我有解决方案的第一部分:
$PATH = $image;
$file = substr(strrchr($PATH, "/"), 1);
echo $file;
给了我:
有人可以告诉我,如何从字符串中删除至少“-01.jpg”吗?
谢谢!
答案 0 :(得分:1)
试试这个,它使用preg_replace:
$arr = ["Wildebeest-01.jpg", "greater-kudu-02.jpg", "BLUE-AND-YELLOW-MACAW-08.jpg"];
foreach ($arr as $string) {
$string = preg_replace("/-[^-]*$/", "", $string);
$string = str_replace("-", " ", $string);
$string = strtolower($string);
var_dump($string);
}
答案 1 :(得分:0)
您有正确的想法来获取文件
$file = substr($PATH,strrpos($PATH, "/"));
然后在最后-
之后删除所有内容。
$file = substr($file,0,strrpos($file,'-'));
然后将-
转为。
$file = str_replace('-',' ',$file);
修改强>
如果您不关心将来可能发生的变化,例如更大的数字,不同的文件扩展名等。你可以这么做。
$file = substr($file,0,-5);
答案 2 :(得分:0)
继续你的解决方案..
$PATH = $image;
$file = substr(strrchr($PATH, "/"), 1);
$op = preg_replace("/([a-zA-Z-]+).*/", "$1", $file);
$filename = trim( str_replace('-', '', $op);
echo $filename; // outputs Wildebeest
答案 3 :(得分:0)
我的建议:
$array = array();
$array[] = "images/large/Wildebeest-01.jpg";
$array[] = "images/large/greater-kudu-02.jpg";
$array[] = "images/large/BLUE-AND-YELLOW-MACAW-08.jpg";
function get_image_name($path) {
$file = basename($path);
if(preg_match("/(.*?)(-[0-9]*){0,1}([.][a-z]{3})/",$file,$reg)) {
$file = $reg[1];
}
$file = ucfirst(strtolower(str_replace("-"," ",$file)));
return $file;
}
foreach($array as $path) {
echo "<br>".$path;
echo " => ".get_image_name($path);
}
输出是:
images/large/Wildebeest-01.jpg => Wildebeest
images/large/greater-kudu-02.jpg => Greater kudu
images/large/BLUE-AND-YELLOW-MACAW-08.jpg => Blue and yellow macaw
答案 4 :(得分:0)
另一个解决方案,使用preg_match()。:
BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext()).build();
Bitmap bitmap = ((BitmapDrawable) mBarcodeImageView.getDrawable()).getBitmap();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<Barcode> barcodes = detector.detect(frame);
Barcode thisCode = barcodes.valueAt(0);
TextView txtView = (TextView) findViewById(R.id.txtContent);
txtView.setText(thisCode.rawValue);
结果:
$getImageName = function ($path) {
if (preg_match('/([\w\d-_]+)-\d+\.(jpg|jpeg|png|gif)$/i', $path, $matches)) {
return ucwords(strtolower(str_replace(str_split('-_'), ' ', $matches[1])));
}
return false;
};
$paths = array(
'images/large/Wildebeest-01.jpg',
'images/large/greater-kudu-02.jpg',
'images/large/BLUE-AND-YELLOW-MACAW-08.jpg',
);
$names = array_map($getImageName, $paths);
print_r($names);
答案 5 :(得分:0)
谢谢你们!根据您的答案,我找到了完美的解决方案:
$filename = pathinfo($PATH);
$filename = $filename['filename'];
$filename = preg_replace("/-[^-]*$/", "", $filename);
$filename= ucfirst(strtolower(str_replace('-',' ',$filename)));
echo $filename;