5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【php】URLを取得してカレント表示させる

Last updated at Posted at 2014-09-05

例えば下記のURLがあったとして、
http://sample/★★★/
★★★にいるときに、★★★ページのグロナビなどをカレント表示させたいときに使いました。

php

<?php
 function is_current( $uri = "" ) {
    $uri = trim( $uri, "/" );
    $request_uri = $_SERVER['REQUEST_URI'];
 
    if( $uri && strpos($request_uri."/", "".$uri."", 0) !== FALSE ) {
        return true;
    }
    $request_uri = trim(str_replace( "/index.php", "", $request_uri ), '/'); // top
    if( !$uri && !$request_uri ) {
        return true;
    }
    return false;
}
function echo_current( $uri = "" ) { 
    if(is_current( $uri )) {
        echo '_on';
    };
}
?>

php

<ul>
  <li><a href="/"><img src="http://sample/images/gnav_top<?php echo_current("");?>.png" alt="トップページ"></a></li>
  <li><a href="/★★★/"><img src="http://sample/images/gnav_top<?php echo_current("★★★");?>.png" alt="★★★ページ"></a></li>
</ul>

★★★ページ(http://sample/★★★/ )にいる際に、
画像URLのの部分が「_on」に変わります。

引用:地味に便利:自動的にリンクにclass=”current”を付加するPHP書きました。 | Toro_Unit

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?