How do I print out a section of a URL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cre8tor
Forum Newbie
Posts: 18
Joined: Tue Aug 24, 2010 3:58 pm

How do I print out a section of a URL?

Post by Cre8tor »

Can someone tell me how to print out a part of the url that the visitor has landed upon? Specifically the part after the question mark. For example, if the visitor has landed on the url: http://www.url.com/test.html?item=28444 how would I print out just the number 28444?
This is what I know so far. That I could use the parse_url command as follows:

Code: Select all

<?php

$url = parse_url("http://www.url.com/test.html?item=28444");

echo ereg_replace("item=", "", $url["query"]);

?>
This code does print out the number 28444. However, in the above example, I entered the actual url for the $url variable. If I’m going to enter the url myself I might as well use “echo 28444” for the entire code. I think the point for any such code would be to pick off from the url ANY item number, based on the url that the visitor has landed upon (not the url that we have typed ourselves in the code). For example, if the visitor lands on the url: http://www.url.com/test.html?item=28577 , based on the above code it would still print 28444 because I defined the $url variable myself using http://www.url.com/test.html?item=28444

So how could it be coded so that this item number section of the url is printed based on whatever URL the visitor is on?
Last edited by Cre8tor on Fri Sep 03, 2010 9:25 pm, edited 2 times in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Print out a section of a URL

Post by Jonah Bron »

Put this in your page and click the link on the other site.

Code: Select all

<?php
echo $_GET['item'];
?>
Cre8tor
Forum Newbie
Posts: 18
Joined: Tue Aug 24, 2010 3:58 pm

Re: How do I print out a section of a URL?

Post by Cre8tor »

This is assuming that the name of the variable on the other site is called "item", right? I don't think it is, the code for the hyperlink sending to my page is: http://www.url.com/test.html?item={%NUMBER}

Anyway I tried

Code: Select all

<?php
echo $_GET['item'];
?>
and

Code: Select all

<?php
echo $_GET['NUMBER'];
?>
but neither of them are printing anything. Any other idea?
It's interesting because I don't know what kind of variable starts with the % sign in front of it, let alone wrapped in curly brackets { }. I believe this NUMBER is defined in a database.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How do I print out a section of a URL?

Post by Jonah Bron »

{%NUMBER} is just a string that their code has defined as where the number goes. It is not the variable name.

viewtopic.php?f=1&t=120660#p622767
Cre8tor
Forum Newbie
Posts: 18
Joined: Tue Aug 24, 2010 3:58 pm

Re: How do I print out a section of a URL?

Post by Cre8tor »

Hey thanks, it was working after all! The font color was set at a color very close to the background so I couldn't see it at first. Your first suggestion worked. Thanks again!
Cre8tor
Forum Newbie
Posts: 18
Joined: Tue Aug 24, 2010 3:58 pm

Re: How do I print out a section of a URL?

Post by Cre8tor »

Here is a second part to the question which I may just start in a new thread if not answered here: How can I make that number be passed through in a form? Either populating it in a text field, or pass it through some other way?

The form field code would go something like this:

<form method="post" action="my-form-script.php">
<input name="number" id="number" "type="text">
</form>

but how would I incorporate the

<?php
echo $_GET['item'];
?>

into it so it is either displaying inside the text field or again passed through some entirely different other way?

I was thinking along the lines of putting something like: number = echo $_GET['item'] and then passing that variable through the form but I got an error with that line right away.
Post Reply