2

I want to pass a URL to a function. This function should get the node ID from the URL.

How to do this?

6 Answers 6

1

You can try the following code (please note that about-us is merely an example page - you can put whichever page you want here):

$url = 'about-us';
$path = drupal_lookup_path('source', $url);

if ($path) {
  $node = menu_get_object('node', 1, $path);    
  return $node->nid;
}
else {
  //Error handling in case path provided does not exist
}

The code above returns a whole node object though, which might be expensive depending on how often you run it - another option would be to run explode() on the output of drupal_lookup_path() and just take the NID that way.

References:

drupal_lookup_path();

menu_get_object();

1

You may try the following one-liner:

$nid = end((arg())); // Get the last number from the URL.
0

You can read the URL values using arg() function.

For Example,

If your url is node/100 then

$argument_one = arg(0); // This will store "node" to the variable.
$node_id = arg(1); // This will store 100 to $node_id variable.

Like above using arg() function you read the URL components.

Hope it solves your issue !

3
  • what if i wanted to get the node ID of some other URL (refferal)
    – TeAmEr
    Commented Oct 1, 2012 at 9:53
  • What do you mean by some other URL ? Can you post example URL ?
    – Anil Sagar
    Commented Oct 1, 2012 at 9:54
  • yes , for example : $nid = getNodeId($URL); ... i want to pass the URL to a function . i didn't mean with my question that i want to extract the node ID of the current URL
    – TeAmEr
    Commented Oct 1, 2012 at 11:45
0

Theirs tonnes of different ways to "pass the url to a function" which way suits best depends on the situation and preference really.

What is the exact purpose of passing this URL with the node id? is there going to need to be multiple Node ID's passed at any one point? More details would mean a more detailed answer also.

$URL = $_SERVER['REQUEST_URI'];
OR 
$URL = $_GET['q']
// if using this then the full path needs prepending as this only gets node/nid
OR 
$URL = current_path(); // same as GET[q] basically 

// then this passes to that function you wanted
$nid = getNodeId($URL);
1
  • He is asking for a function which will give him node id of any path(alias) passed to the function
    – Gokul N K
    Commented Feb 12, 2014 at 6:20
-1

You could break the url string into an array as

$url_arr = explode('/', $url);

Then you can access the url parts from the array.

1
  • $url variable is not defined by default.
    – saadlulu
    Commented Aug 17, 2013 at 9:54
-3

Pass the variable (node id) appending it to url like this:

    <?php
    $nodeid = $node->nid;
    ?>

<a href="http://www.mysite.com?nodeid="<? php print ($nodeid) ?>">

and then in get the value from url like:

<?php
$nodeid=$_GET["nodeid"];
?>

And then pass the value to the function.

http://php.net/manual/en/reserved.variables.get.php

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.