PHP obtains the code for executing the php file name,

Source: Internet
Author: User

PHP obtains the code for executing the php file name,

This problem occurs in the navigation judgment when I write the confession wall today. My solution is as follows:

Determine the current php file name to determine the highlight of the navigation bar.
So how does php get the current url File Name?
I handle it like this: shock:

Tutorial

First, you must obtain the URL of the current page. Here we use the php$_SERVER['PHP_SELF']To obtain the webpage address.
Assume that the url is a http://www.bkjia.com/index.php

$ Php_Self = $ _ SERVER ['php _ SELF ']; // get Web address // output result: http://www.bkjia.com/index.php

After the output, we found that we only need index. php, and the previous one is useless.
What should we do?
Here we use the substr () function.
substr() Is a part of the returned string:

Substr syntax
Substr (string, start, length)

Parameters Description
String Required. Specifies that a part of the string is to be returned.
Start

Required. Specifies where the string starts.

  • Positive number-start at the specified position of the string
  • Negative number-start at the specified position starting from the end of the string
  • 0-Start at the first character in the string
Length

Optional. Specifies the length of the string to be returned. The default value is until the end of the string.

  • Positive number-length returned from the position of the start Parameter
  • Negative number-length returned from the end of the string

The second parameter of the function specifies where the string starts. Obviously, the last/start in the url is what we need.
So we need to use strrpos()Function.

Strrpos syntax

Strrpos (string, find, start)

Parameters Description
String Required. Specifies the string to be searched.
Find Required. Specifies the characters to be searched.
Start Optional. Specifies where to start searching.

The Code is as follows:

$ Php_Self = substr ($ _ SERVER ['php _ SELF '], strripos ($ _ SERVER ['php _ SELF'], "/") + 1 ); // Why add 1? Because you need to exclude the previous/

A recommended Function

// Obtain the current Script URL function GetCurUrl () {if (! Empty ($ _ SERVER ["REQUEST_URI"]) {$ scriptName = $ _ SERVER ["REQUEST_URI"]; $ nowurl = $ scriptName ;} else {$ scriptName = $ _ SERVER ["PHP_SELF"]; if (empty ($ _ SERVER ["QUERY_STRING"]) {$ nowurl = $ scriptName ;} else {$ nowurl = $ scriptName. "? ". $ _ SERVER [" QUERY_STRING "] ;}} return $ nowurl ;}

If you feel the above article is not fine enough, we suggest you read this article: http://www.bkjia.com/article/22056.htm

This solves the problem. In many cases, we need the help of some string truncation functions to get the desired results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.