Acquisition of directory name (explode function of PHP)

We would sometimes like to acquire the directory (folder) name of a web page by PHP. By this, PHP script can be simplified or the number of files can be reduced. I used the directory name acquired by PHP for the log file name of access analysis.

The directory name of a file is acquired using PHP.

Seemingly, the split() function became deprecated by PHP 5.3.0. An explode() function is used instead. It seems that operation is quicker for this.

PHP explode() function is used.

The function of explode() returns the array of the character string which divided the character string by the delimiter ($delimiter).


Each element of this array becomes the partial character string which divided the original character string ($string) by the character string ($delimiter).

The element of the maximum limit number is contained and, as for the arrangement to which it is returned when a positive value is specified as limit, all the remaining portions of $string are contained in the element of the last.

When limit is negative, except for a limit piece element, all the components are returned from back.

The directory name of a file is acquired using PHP explode() function.


"$_SERVER['SCRIPT_NAME']" expresses the server's path and file name of the back of domain name. For example, if it is "hogehoge. jp/dir1/dir2/example.php", "/dir1/dir2/example.php" is expressed. If the directory (folder) name "dir1" in this is taken out, it is considered as "$dir_a[1]". If "dir2" in this is taken out, it is considered as "$dir_a[2]".

Since it will become an error if it is going to direct-explode(), "$_SERVER['SCRIPT_NAME']" has been once changed into "$s_name".

The example which applied the acquired directory name to the log file name of access analysis

The script of access analysis called "fstat" is as follows. This is described in the body tag of each file. This is made into an external file, and it includes and calls by PHP. My website divides the directory (folder) for every genre, and access analysis is also performing it for every directory. This method is very convenient.

At the following script, a log file name is replacing by "<?php echo $dir_a[1]; ?>".

<script type="text/javascript">
  buf = escape(parent.document.referrer);
  ref = "";
  for (i = 0; i < buf.length; i++) {
  str = buf.charAt(i);
  ref += (str == "+") ? "%2B" : str;
  scr = screen.width+","+screen.height+","+screen.colorDepth;
  document.write('<img SRC="***/***/hoge.cgi?LOG=<?php echo $dir_a[1]; ?>" />');
// -->
<noscript><img src="***/***/hoge.cgi?LOG=<?php echo $dir_a[1]; ?>" /></noscript>

When the acquired directory name was applied to the log file name of access analysis, the required file could be managed with one for every log file name until now. That is, the required include file (about 20-30 pieces) became one piece for every genre.

Example which generated the Rakuten advertisement script automatically using the keyword

The keyword group of a web page is registered by PHP, and in my case, it is using it, acquiring as a keyword of a meta-tag. Two keywords are acquired from before this keyword group, and the script of the Rakuten advertisement is generated automatically.

Since keywords differ on each page if it carries out by this method, what has a code separate on each page is generated. Moreover, since the optimal advertisement comes out on each page, it seems that an effect is large. The advertisement which matched the contents of each page comes out like the link unit of Google.