I have a pretty simple question about PHP that I hope you'll be so kind as to help me out with. Basically, I'd like to be able to echo the text after a "?" mark on my page from the URL. So, if a user types in http://www.digitalpoint.com/?hey there I'd like to be able to display the words "hey there" on the page, or anything else that follows the "?" I've tried a script for doing this, but it treats all spaces like it would a URL, it shows "hey%20there", instead of "hey there". I much appreciate any help you can give, thank you very much in advance.
you could make it easier on yourself. Make the urls print out liek this: www.mydomain.com?v=hey_there You can change the v= to whatever you like, just remember what it is, you can also if you like add in other variables: www.mydomain.com?v-hey_there&y=hey_back Then to echo out these into your content, you simply need to add: print($_GET[v]); //which will print hey_there print($_GET[y]); //which will print hey_back Code (markup): The above replies function print_r($GET); is a function to print the contents of an array (the array being between the brackets, and in theis instance the $_GET variables which is the name given to variables contained inside the url) Hope that helps.
The way the information is posted to the server by the URL is this: http://domain.ext/?name=value There is an easy way to redirect all your /?hey there requests via the mod_rewrite. Just add this rule in your .htaccess file in the root of your domain: RewriteRule ^domain\.ext/\?([a-z]+)$ /hi.php?name=$1 Where hi.php is the file you use to echo the text. Your visitors will see the URL: http://domain.ext/?sterex however, all the action will be happening at 'hi.php?name=sterex'
Put this into a file: <? foreach ($_GET as $key=>$value) { echo $key; } ?> PHP: E.g. if you put it into text.php, you can do it with text.php?hey Try it yourself: http://pastedrop.com/hey.php?hello
BEAUTIFUL!!!! You've saved my life man. Or solved a very frustrating problem for me. Same difference. Thank you.
Yeah, but if you want to do it a shorter way, without an equalization mark, use the code I wrote in #6. With logondotinfo's code, you have to use e.g. ?text=hello, with mine, you can do it with ?hello And you shouldn't use print($_GET[v]); too, use echo $_GET[v]; PHP: instead.
P.S. If you just want to get the stuff after the ? use $_SERVER['QUERY_STRING']; That would give you hey there out of http://domain.com/index.php?hey there
I tried this one, but it adds an underscore between each word, eg "hey_there" I'm correctly trying all suggestions, thanks.
<?php $word= explode("?", $_SERVER['REQUEST_URI']); $word = urldecode($word[1]); echo $word;?> Better still if you have spaces then you will get urlencode, so urldecode it
<? foreach ($_GET as $key=>$value) { echo str_replace("_"," ",$key); } ?> You would have to replace to "_" with " "
<?php $word= explode("?", $_SERVER['REQUEST_URI']); $word = urldecode($word[1]); echo $word; ?> This would take about half the time to process than the foreach
No, he don't. My code was 100% correct. He want's to replace _ with a line space. It does that. Test it yourself. The correct code is still: <? foreach ($_GET as $key=>$value) { echo str_replace("_"," ",$key); } ?> PHP: