how to get the words in the url?

breaddawson - February 28, 2008 - 09:49

i want the theme to display different elements for different urls,
for example, if the url is /a/* ,it prints a "hello, a"
if the url is /b/* ,it prints a "hello, b"
but how can i get the words "a" or "b" from the url?
is that a function?

You can use; <?php  print

pobster - February 28, 2008 - 10:06

You can use;

<?php
 
print arg(1);
?>

http://api.drupal.org/api/function/arg/6

Obviously that only works if you know which part of the url holds the information you want to display... If you don't then pay close attention to how arg creates the result (from the api page above).

Pobster

it doesn't work to Alias for

breaddawson - February 28, 2008 - 11:53

it doesn't work to Alias
for example, i have a link: /node/11
then i made an alias called /abc to it
then arg(0) just return node, not abc

...Hmmmm what does arg(1)

pobster - February 28, 2008 - 11:59

...Hmmmm what does arg(1) return?

Pobster

------url-------alias----

breaddawson - February 28, 2008 - 12:33

------url-------alias----
/node/11 /abc

arg(0) return node
arg(1) return 11

Okay;

pobster - February 28, 2008 - 12:42

Okay;

<?php
 
print $_GET['q'];
?>

What does that return?

...Incidentally... When you're running the previous print statement, are you trying it from both /node/11 and /abc? You are able to access both...

Pobster

arg() will always return the

dman - March 3, 2008 - 11:18

arg() will always return the system path (node/n)
that makes it actually useful for coders, no matter what aliases have been doing.

$_REQUEST['q'] and similar will still have the aliased versions.
You'll have to split() it yourself or something probably.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Q

jari - February 28, 2008 - 12:49

<?php
$path_pieces
= explode('/', $_GET['q']);
print_r($path_pieces);
?>

so if your url is http://www.example.com/qwerty, then $path_pieces[0] would print "qwerty"

the function explode()

breaddawson - February 28, 2008 - 12:55

the function explode() return an array?
i tried ur code and ouput $path_pieces ,then i get an array
when i output $path_pieces[0], i still get "node"
maybe it's because /node/11 is the real address?
when drupal get /abc it then alter to /node/11...

omg....the alias confused me...

That's not helpful...

pobster - February 28, 2008 - 12:58

That's not helpful... Follow the thread, the Drupal function arg() uses that very same $_GET['q'] with explode to create its result. It's the alias he's after which I'm wondering whether he's actually set up correctly or not?...

A more helpful post would have been to just get him to use drupal_get_path_alias...

Pobster
edit: Okay so you replied before I did... Really... Just ignore that post...

Just try using;

pobster - February 28, 2008 - 13:02

Just try using;

<?php
 
print drupal_get_path_alias($_GET['q']);
?>

It's not entirely ideal... But then I'm not entirely sure exactly what you're trying to achieve. Anyways, it'll certainly tell me whether you've set the alias up correctly or not...

Pobster

it

breaddawson - February 28, 2008 - 13:05

it worked!
drupal_get_path_alias($_GET['q']) just return the alias of the path!
thank u very much for your help!!

$_SERVER['REQUEST_URI'] instead of $_GET['q']

jari - March 2, 2008 - 19:59

hehe, I was about to put $_SERVER['REQUEST_URI'] instead of $_GET['q'] to my example code, but then read your post about it and $_GET['q'] gave the same thing - guess it was just a bad test node :)
$_SERVER['REQUEST_URI'] should always return the path, like when writing this it would print "comment/reply/227849/750216" (not sure about the leading slash though) - maybe that is also faster, though of course we're talking about fractions of milliseconds on getting the alias from the database.

thank u for your

breaddawson - March 3, 2008 - 09:44

thank u for your reply...
but my situation is that ,i want to get part of the path, for example, the url is /abc/def
i just want to get abc
can $_server['request_uri'] do this for me?
and....u mean $_GET['q'] can not always return the right value?

I was steering clear of that

pobster - March 3, 2008 - 10:46

I was steering clear of that since it's only available with Apache. When giving advice for code you should never assume anything.

The code I gave using $_GET['q'] will work with any web server and whilst being a slightly slower way to do it, it's more compatible should someone be searching for this issue and they're using something else...

Pobster

combining the reply advices

jari - March 5, 2008 - 07:29

Ok, then just combine two replies (http://drupal.org/node/227849#comment-750198 and http://drupal.org/node/227849#comment-750216) together.

<?php
 
//explode() will split the given string with another string and put the pieces into an array
 
$path_pieces = explode('/', drupal_get_path_alias($_GET['q']));

 
//so if your path is /abc/def, $path_pieces is now like Array ( [0] => abc [1] => def )

  //Therefore, $path_pieces[0] prints 'abc' that you wanted
 
print $path_pieces[0];
?>

Hope we covered it this time ;)

I was able to use this well.

Rob T - May 25, 2008 - 05:18

I was able to use this well. Thanks for the snippet.

Could this peace of code

Summit - June 23, 2008 - 15:30

Could this peace of code also be used in node-body, to have a sort of solution like a template.
So have normal tekst and because of the url, have some text inserted.
Example:
url is www.example.com/economics

This is a example text about [peace of code]

The node should than be:

This is a example text about economics

This is working, great! Any issues with this may be?

greetings,
Martijn

$_SERVER['SCRIPT NAME'];

stripped your speech - June 23, 2008 - 14:28

$_SERVER['SCRIPT NAME']; will do this as well.

Trust me, I do this on all our websites to display certain elements in certain sections or pages. It works.

Could you give an example

Summit - June 23, 2008 - 19:09

Could you give an example please of using $_SERVER['SCRIPT_NAME']; ?
Thanks in advance,
greetings,
Martijn

ok: <?php $thisPageURL =

stripped your speech - June 23, 2008 - 19:34

ok:

<?php $thisPageURL = str_replace("q=", "", $_SERVER['QUERY_STRING']); $thisPageURL = explode("/", $thisPageURL); ?>

To see what it grabs, do print_r($thisPageURL);. Explode puts each word into an array.

edit: sorry, i meant QUERY STRING and not SCRIPT NAME

i meant QUERY STRING and not

dman - June 23, 2008 - 21:47

i meant QUERY STRING and not SCRIPT NAME

... um, yeah. I was really starting to wonder how you'd be getting THAT to work ;-)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

 
 

Drupal is a registered trademark of Dries Buytaert.