1

I am trying to get matched parts by comparing

$_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF'];

Like if $_SERVER['DOCUMENT_ROOT'] returned: var/www/subdir

and $_SERVER['PHP_SELF']; returned /subdir/index.php/somepage

so how do i match $_SERVER['DOCUMENT_ROOT'] from right

and $_SERVER['PHP_SELF']; from left and then get the matched part

So that I will get this in output: subdir

Thanks for help

1
  • >I am trying get base url of a application by comparing $_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF']; Like if $_SERVER['DOCUMENT_ROOT'] returned: var/www/subdir and $_SERVER['PHP_SELF']; returned /subdir/index.php/somepage so how do i match $_SERVER['DOCUMENT_ROOT'] from right and $_SERVER['PHP_SELF']; from left and return matched. please can u clear what u exactly wanting...didnot understand Commented Jun 3, 2014 at 8:14

2 Answers 2

1

i will advice you to use preg_match. using it you can get the string before first / of $_SERVER['PHP_SELF'] and string after last '/' of $_SERVER['DOCUMENT_ROOT'] and then compare them when a match is found then it will be subdir in your case

preg_match documentations ::

  1. php.net/preg_match
  2. tutorialspoint.com/php/php_preg_match.htm
  3. preg_match EXAMPLES

for additional information you can also get your root directory using

define('DIR_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR); in your php file which is kept in your root directory(i.e. index.php,hopefully)... just saying if it helps you

Sign up to request clarification or add additional context in comments.

Comments

0

If you want the base url, than you are trying the wrong thing. You need to try this

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

This question is already answered here :

How do I get the base URL with PHP?

OR refer to the PHP documentation here

2 Comments

NO this will not work for me, cuz if I try to get REQUESTED_URI from sub directory then this will give current directory.
If I am not wrong you are trying this ? $ss = 'var/www/subdir'; $ps = '/subdir/index.php/'; $ss = end(explode('/', $ss)); $ps = explode('/', $ps); $ps = $ps[1]; $foo = null; if($ps == $ss){ $foo = $ps; } echo $foo;

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.