When viewing a page at the path "admin/structure/types", for example, arg(0)
returns "admin", arg(1) returns "structure", and arg(2) returns "types".

Avoid use of this function where possible, as resulting code is hard to
read. In menu callback functions, attempt to use named arguments. See the
explanation in menu.inc for how to construct callbacks that take arguments.
When attempting to use this function to load an element from the current
path, e.g. loading the node on a node page, use menu_get_object() instead.

Parameters

$index:
The index of the component, where each component is separated by a '/'
(forward-slash), and where the first component has an index of 0 (zero).

$path:
A path to break into components. Defaults to the path of the current page.

Return value

The component specified by $index, or NULL if the specified component was
not found. If called without arguments, it returns an array containing all
the components of the current path.

File

Code

functionarg($index = NULL, $path = NULL) {
// Even though $arguments doesn't need to be resettable for any functional
// reasons (the result of explode() does not depend on any run-time
// information), it should be resettable anyway in case a module needs to
// free up the memory used by it.
// Use the advanced drupal_static() pattern, since this is called very often.static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['arguments'] =& drupal_static(__FUNCTION__);
}
$arguments =& $drupal_static_fast['arguments'];
if (!isset($path)) {
$path = $_GET['q'];
}
if (!isset($arguments[$path])) {
$arguments[$path] = explode('/', $path);
}
if (!isset($index)) {
return$arguments[$path];
}
if (isset($arguments[$path][$index])) {
return$arguments[$path][$index];
}
}

Comments

I have begun to avoid using this function as it does not always work as you expect (at least from the description above).

Example:
Current page URL: /moduleX/doSomething
This URL refers to a function in ModuleX. In ModuleX arg(0) will be "moduleX" and arg(1) will be "doSomething".

But if ModuleX calls a function in AnotherModuleY, for some reason arg(0) and arg(1) don't return the same as when they are in ModuleX - they seem to return a URL that might have (theoretically) been used to call that module directly, such as "AnotherModuleY" and "doSomethingDifferent".

Since I was using PathAuto, arg() was not returning the URL path, but the internal arguments. Using your approach allowed me to get the segments of the URL as printed in the browser with request_uri() and find my path that way.. You rule!

I just had an issue where this was being used in a custom form for a new node where some info was being passed to the form through the URL (without any contribs). It appeared to be working fine for quite awhile. But then started failing sporadically.

Turns out it was failing due to ajax calls from the form (unlimited multi-value fields, file fields, etc) because drupal rewrites the URL for those calls.

Exact usage will depend on the structure of the url you're trying to parse, but the idea is to get the info from the url only when it's not an ajax call, and store it in the form for when it is an ajax call.