basename() public static method

Returns path component of path.
Given a string containing a path to a file, this function will return the base name of the file. If the path ends in this will also be cut off. On Windows, both slash (/) and backslash (\) are used as path separator character. In other environments, it is the forward slash (/).
public static basename ( string path, string suffix = null ) : string
path string A containing a path to a file.
suffix string A containing suffix to be cut off the path if present.
return string
Ejemplo n.º 1
0
        /// <summary>
        /// Extracts part(s) from a specified path.
        /// </summary>
        /// <param name="path">The path to be parsed.</param>
        /// <param name="options">Flags determining the result.</param>
        /// <returns>
        /// If <paramref name="options"/> is <see cref="PathInfoOptions.All"/> then returns array
        /// keyed by <c>"dirname"</c>, <c>"basename"</c>, and <c>"extension"</c>. Otherwise,
        /// it returns string value containing a single part of the path.
        /// </returns>
        public static PhpValue pathinfo(string path, PathInfoOptions options = PathInfoOptions.All)
        {
            // collect strings
            string dirname = null, basename = null, extension = null, filename = null;

            if ((options & PathInfoOptions.BaseName) != 0 ||
                (options & PathInfoOptions.Extension) != 0 ||
                (options & PathInfoOptions.FileName) != 0)
            {
                basename = PhpPath.basename(path);
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                dirname = PhpPath.dirname(path);
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    extension = basename.Substring(last_dot + 1);
                }
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    filename = basename.Substring(0, last_dot);
                }
                else
                {
                    filename = basename;
                }
            }

            // return requested value or all of them in an associative array
            if (options == PathInfoOptions.All)
            {
                var result = new PhpArray(4);
                result.Add("dirname", dirname);
                result.Add("basename", basename);
                result.Add("extension", extension);
                result.Add("filename", filename);
                return(PhpValue.Create(result));
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                return(PhpValue.Create(dirname));
            }

            if ((options & PathInfoOptions.BaseName) != 0)
            {
                return(PhpValue.Create(basename));
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                return(PhpValue.Create(extension));
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                return(PhpValue.Create(filename));
            }

            return(PhpValue.Null);
        }