0

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> &raquo; ");
    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> &raquo; ");
        } 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

5
  • $file = 'this-shirt.pdf'; $filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1)))); echo $filename; Commented Feb 14, 2017 at 14:57
  • @MonkeyZeus dear can you please post your code on answer? because it is not clear like this Commented Feb 14, 2017 at 15:23
  • Which variable contains this-shirt with the extension? Commented Feb 14, 2017 at 15:34
  • this-shirt is file name like this this-shirt.php but everytime i put this whole code on a page i created i have to change it to the current page name. instead of that all i want php grab the file name by itself there. i don't want to create for example file with name my-file.php and place that whole code on that page and change this-shirt to my-file Commented Feb 14, 2017 at 15:37
  • I see. Please see my answer below. Commented Feb 14, 2017 at 15:48

1 Answer 1

1

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
);
Sign up to request clarification or add additional context in comments.

2 Comments

worked bro. exactly like i wanted. thank you so much
dear can you hhelp me with this stackoverflow.com/questions/42262290/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.