Reputation: 227
what i'm trying to do here, in my php code below i have to set file name manually and i want to make it some how it grabds the file name automatically but without file extention
here's part of my code where i want to get file name
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
'this-shirt' => 'This Shirt'
);
where it says "this-shirt" is file name and i want it to be set automatically instead of i write it down everytime i create a page. also here's full code
<?php
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
'this-shirt' => 'This Shirt'
);
$path = $_SERVER["PHP_SELF"];
$parts = explode('/', $path);
if (count($parts) < 2) {
echo("home");
} else {
echo ("<a href=\"http://domain.com\">Home</a> » ");
for ($i = 2; $i < count($parts); $i++) {
if (!strstr($parts[$i], ".")) {
echo("<a href=\"");
for ($j = 0; $j <= $i; $j++) {
echo $parts[$j] . "/";
};
echo("\">" . str_replace('-', ' ', $Pages[$parts[$i]]) . "</a> » ");
} else {
$str = $parts[$i];
$pos = strrpos($str, ".");
$parts[$i] = substr($str, 0, $pos);
echo str_replace('-', ' ', $Pages[$parts[$i]]);
}
}
}
hope you get the idea. thanks
Upvotes: 0
Views: 84
Reputation: 20737
This should do it:
// get this-shirt.php from the URL
$file = basename($_SERVER["PHP_SELF"]);
// pure magic
$filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1))));
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
$filename => 'This Shirt' // use $filename to declare the array's key
);
Upvotes: 1