Hello, If I want to extract football from this url: mysite.com/news/football.html how do I do it? Is there a simple GET command I can use? Thanks, Luke
I would probably use a regular expression assuming that the directory is always in the same location in the url. Use a preg_replace expression and you can strip everything except what is between the / and the .html.
preg_replace("|/(\\w+)\\.\\w+$|",$url,$regs); $regs[1] would containt "football" assuming $url was "mysite.com/news/football.html"
ds316, what if there is a hyphen, e.g. table-tennis, your regex fails. Also, you should be using preg_match(), not preg_replace() nico's one should be fine, I assume all the pages will have .html, however if they don't, you might want to use regex... or what he mentions below
I'm too tired, i shouldnt be coding. I did write it in a hurry, but yes you are entirely correct, the better implementation would be: preg_match("|^[^\\?]+/([^\\./]+)\\.|",$url,$regs); Again $regs[1] conatins your name. That covers all bases, even if there was a query string with an unencoded url such as: http://mysite.com/news/football.php?refurl=http://google.com/search?q=football Still, I think basename covers it in that situation as well, its up to you which method to use.
Thanks very much for your answers. Something crazy is happening on my site. The following will not print: $url = urlencode($_GET['url']); $foo = basename($url, '.' . end(explode('.', $url))); print $foo; If you have any ideas why this is not working, please let me know. Thanks
Ok, I've resolved it using $url = ($_SERVER['REQUEST_URI']); It's now working. The only thing is that it prints the %20 if there are spaces in that part of the url. Is there any way to take this out? Thanks for your help anyway. Luke
Nico, I'm using the code you gave me to do a redirect: $url = ($_GET['url']); $new_url = rawurldecode(rawurldecode(end(explode('*', $url)))); header('Refresh: 10; url=http://linkanon.com/?r=$new_url'); Do you know why it won't work. It redirects, but to http://linkanon.com/?r=$new_url. It's not replacing the $new_url.
Since you are using ' instead of " it won't process the code properly. Try this: $url = ($_GET['url']); $new_url = rawurldecode(rawurldecode(end(explode('*', $url)))); header('Location: http://linkanon.com/?r='.$new_url.''); PHP: Header("Refresh is an obsolete method of redirecting. It may work but is most likely going to stop working at some point. I suggest doing the redirect as I wrote above, or using a <META HTTP-EQUIV="Refresh" if you want the delay. That would look like: $url = ($_GET['url']); $new_url = rawurldecode(rawurldecode(end(explode('*', $url)))); echo '<meta http-equiv="refresh" content="10; url=http://linkanon.com/?r='.$new_url.'" />'; PHP: You would need to echo somewhere after <head> and before </head>
Thanks for your answer. Am I right that search engine spiders follow both these two types of redirects - meta and header location? Is so, Javascript will be better for me in this case. I want the spider to index the contents of my page, but I want the visitor to be immediately redirected.