Im using an angular script for my site.
I need dynamic titles so I decided to use php to fetch the last part of the url to display for the page title.
This is the code im using. This code is out putting my full url.
<?php
function curPageURL() {
$url = $_SERVER['REQUEST_URI'];
$url_array = explode('/',$link);
$lastPart = array_pop($url);
return $lastPart;
}
echo curPageURL();
?>
The problem is its fetching the whole url.
For example here is my url http://www.istreamradio.ie/category/Music
I need to display just the last part of the url in this case is Music.
Thanks In advance.
You can use end() function after exploding the URL . Please try end instead of array_pop.
Related
I'd like to insert part of the page url where the php function is displayed inside the brackets of the function. For example, my page's url is: www.mydomain.com/directory/**page**
I'd like to take /page and insert it inside: function(here)
Wont this do?
<?php
$url = "www.mydomain.com/directory/page";
$url = explode('/', trim($url, '/'));
print( $url[count($url)-1]);//prints "page"
//somefunc(page)
?>
Split the url by by / and select the last part of it. Then you can use it in any function.
On my page I have this piece of code:
<?php
$homepage = file_get_contents('http://www.nbc.com/');
echo $homepage;
?>
It includes the NBC website perfectly, but I notice that all of the links on the NBC.com website start with my domain name instead of http://nbc.com so they don't work.
So for example instead of http://nbc.com/the-blacklist/episodes being displayed on http://nbc.com website, its displaying http://my-domain.com/the-blacklist/episodes.
Is there any way to use file_get_contents do include a page into my URL, but make sure all the links on the page are the original links so they work fine?
The links inside of pages you are pulling are using relative links (/page.html) instead of the full URL, you will need to do a Regex on the variable or string replace. To test it out you could do the following:
$domain = 'http://www.nbc.com';
$pull = file_get_contents($domain);
echo str_replace('href="/', 'href="' . $domain . '/', $pull);
I am sending a link in parameter to a php page like :
http:// exemple.com/test.php?url=http:// i-want-to-get-this-link.com
in php Page I get this link like this :
$_GET['url'];
the problem some time the link has & symbol :
http:// exemple.com/test.php?url=http:// i-want-to-get-this-link .com/?page1&desc/otherstaff
so I only get the first part of the link : http:// i-want-to-get-this-link.com/?page1
how could I get the rest of the link &desc/otherstaff
Use urlencode on your url variable to generate the url
<?php
$url = 'http:// exemple.com/test.php?url='.urlencode('http://i-want-to-get-this-link.com');
?>
EDIT : you do not need urldecode with $_GET and $_REQUEST variables.
and urldecode to get the original URL.
<?php
$url = urldecode($_GET['url']);
?>
When you create your whole url, try to urlencode it:
http://php.net/manual/en/function.urlencode.php
then you can use:
urldecode($_GET['url']);
You can use parse_url() function to get the query, if you cannot control the input url. Try this:
print_r(parse_url($_SERVER['REQUEST_URI']));
Think my current page url is:
http://example.com/id=10
In this this page has link to go other page, I want to pass current URL as a query string like this:
http://example.com/about-us/?edit=1&return=http://example.com/id=10
in PHP
http://example.com/about-us/?edit=1&return=<?php echo $_SERVER['QUERY_STRING'] ?>
but this is not working, could anyone help me to do this.
Use this (I assume you are using http only);
$currentUrl = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
$link = "http://example.com/about-us/?edit=1&return=" . $currentUrl;
use urlencode($_SERVER['QUERY_STRING'])
It encodes the link.
I've made a website where users can submit their Facebook page and I orginally coded it assuming that they would of set up a vanity URL for their page. E.g.
http://www.facebook.com/myfacebookpage
But I forgot that some pages can have urls such as
http://www.facebook.com/pages/myfacebookpage/54878424154545487
My question is how do I use PHP to grab the part of the url in bold?
Thanks!
You can just recode your application to use https://graph.facebook.com/?ids=
Which will take both
http://www.facebook.com/myfacebookpage and
http://www.facebook.com/pages/myfacebookpage/54878424154545487
Examples
https://graph.facebook.com/?ids=https://www.facebook.com/boo
https://graph.facebook.com/?ids=https://www.facebook.com/pages/phwd/113702895386410
like this:
$url='http://www.facebook.com/pages/myfacebookpage/54878424154545487'; // the input
$pieces = explode('/', $url); // divides the string in pieces where '/' is found
$key=end($pieces); //takes the last piece