Page 1 of 1
How do I print out a section of a URL?
Posted: Fri Sep 03, 2010 9:18 pm
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?
Re: Print out a section of a URL
Posted: Fri Sep 03, 2010 9:20 pm
by Jonah Bron
Put this in your page and click the link on the other site.
Re: How do I print out a section of a URL?
Posted: Fri Sep 03, 2010 9:47 pm
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
and
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.
Re: How do I print out a section of a URL?
Posted: Fri Sep 03, 2010 9:59 pm
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
Re: How do I print out a section of a URL?
Posted: Fri Sep 03, 2010 10:01 pm
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!
Re: How do I print out a section of a URL?
Posted: Sat Sep 04, 2010 1:24 am
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.