Examine Part of URL

Hello all. I'm trying to figure out how to parse part of a url to determine the associated directory. For example, the URL is:


I need to determine if the URL contains 'admin' or 'user'.

PHP 5.4

Who is Participating?
Beverley PortlockConnect With a Mentor Commented:

$bits = parse_url("http://example.com/user/somedir/index.html");

if ( preg_match( '#^/admin/.*?$#', $bits['path'] ) )
     echo "contains 'admin'";

if ( preg_match( '#^/user/.*?$#', $bits['path'] ) )
     echo "contains 'user'";

Open in new window

Beverley PortlockCommented:
This is probably a good place to start

Steven HarrisPresidentCommented:
Have you looked into dirname() for php4 and php5?

string dirname ( string $path )
Ray PaseurCommented:
You can find this information in $_SERVER["HTTP_HOST"] and $_SERVER["REQUEST_URI"].  The HTTP_HOST will be mywebsite.com and the REQUEST_URI will be /admin/something.php

But that said, whenever I see a question like this, I wonder about the back-story.  Why does your PHP script need to know this information?  There is almost always some reason that can build a security flaw into your design.
formadmirerAuthor Commented:
This worked perfect for my needs - thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.