Reputation: 23
I´m searching solution for this:
I have a paths:
$PATH = images/large/Wildebeest-01.jpg
$PATH = images/large/greater-kudu-02.jpg
$PATH = images/large/BLUE-AND-YELLOW-MACAW-08.jpg
Which I need is this:
"Wildebeest"
"Greater kudu"
"Blue and yellow macaw"
i have a first part of the solution:
$PATH = $image;
$file = substr(strrchr($PATH, "/"), 1);
echo $file;
which gives me:
Could someone advise me, how to remove at least "-01.jpg" from the string?
Thanks!
Upvotes: 1
Views: 1637
Reputation: 23
Thanks guys! Based on your answers, I found the perfect solution:
$filename = pathinfo($PATH);
$filename = $filename['filename'];
$filename = preg_replace("/-[^-]*$/", "", $filename);
$filename= ucfirst(strtolower(str_replace('-',' ',$filename)));
echo $filename;
Upvotes: 0
Reputation: 973
Yet another solution, using preg_match().:
$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);
Result:
Array
(
[0] => Wildebeest
[1] => Greater Kudu
[2] => Blue And Yellow Macaw
)
Upvotes: 0
Reputation: 2698
My suggestion:
$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);
}
Output is:
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
Upvotes: 0
Reputation: 1545
Continuing with your solution..
$PATH = $image;
$file = substr(strrchr($PATH, "/"), 1);
$op = preg_replace("/([a-zA-Z-]+).*/", "$1", $file);
$filename = trim( str_replace('-', '', $op);
echo $filename; // outputs Wildebeest
Upvotes: 0
Reputation: 2464
You have the right idea for getting the file
$file = substr($PATH,strrpos($PATH, "/"));
Then to get rid of everything after the last -
simply do.
$file = substr($file,0,strrpos($file,'-'));
Then turn -
into .
$file = str_replace('-',' ',$file);
EDIT
If you don't care about possible changes in the future, like bigger numbers, different file extensions, and more. You can simply do.
$file = substr($file,0,-5);
Upvotes: 0
Reputation: 12132
Try this, it uses 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);
}
Upvotes: 1