I'm attempting to retrieve the last part of a URL before the trailing backslash. I have used this previously, which worked great, but the URL on the site I was developing back then did not have a trailing slash. Below is the code I used for that.
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
echo $page = end($link_array);
Any help would be appreciated,
Kind Regards,
Rees
This works for me
$link = $_SERVER["REQUEST_URI"];
if(substr($link, -1) == '/') {
$link = substr($link, 0, -1);
}
$link_array = explode('/',$link);
echo $page = strtoupper(end($link_array));
you could try :
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
$lastPart = str_replace('/', '', $link_array[count($link_array) - 1]);
You are almost there. You have to pick the second last value:
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
end($link_array);//move cursor to the end
//pick last or second last depending on trailing slash
$page = substr($link,-1) == "/" ? prev($link_array) : current($link_array);
echo $page;
You can use php's parse_url to parse the url and get the wanted components.
or
EDIT:
$url = 'http://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
if (substr("url", -1) == '/') {
rtrim($url , "/")
}
$lastPart = substr($url, strrpos($url, '/') + 1);
This is from Stackoverflow posts:
Get the full URL in PHP
Get characters after last / in url
Related
I want to convert a URL to regular expression to match it with current URL. For example, I have a URL http://www.example.com/example.php
I want it to convert to
^(https?://)?(www\.)?example\.com\/example\.php/?(\?.)?(#.)?$
So that I store it and whenever a user hits this url with any number of parameters attached to it, I will match that url with my regular expression and will perform some action based on the results.
I have found many questions but they all are either to match general URL (with any domain name) or with regular expressions given. But I want a function to which I will pass URL and it will return its regular expression and I will use it to match that specific domain.
I have finally created this code with the help of stackoverflow and other communities. This provides me the exact string I require against given URL.
<?php
function createrRegex($url) {
$var1 = '^(https?://)?';
$host = parse_url($url, PHP_URL_HOST);
$host_parts = explode('.', $host);
if (!empty($host_parts)) {
$length = count($host_parts);
foreach ($host_parts as $i => $part) {
if ($i == 0) {
if ($part == "www") {
$var1 .= '(' . $part . '\\\\.)?';
} else {
$var1 .= '' . $part;
$var1 .= ($i < ($length - 1)) ? '\\\\.' : '';
}
} else {
$var1 .= '' . $part;
$var1 .= ($i < ($length - 1)) ? '\\\\.' : '';
}
}
}
$path = '';
if ((parse_url($url, PHP_URL_PATH) != NULL)) {
$path = str_replace('/', '\\\\/', parse_url($url, PHP_URL_PATH));
$path = str_replace('.', '\\\\.', $path);
}
$var1 .= $path;
$var1 .= '/?(\\\\?.*)?(#.*)?$';
return $var1;
}
?>
Say if I have two strings
$first = 'http://www.example.com';
$second = 'www.example.com/';
How could I determine they match? I just care that the example part matches. I'm thinking some form of Regex pattern would match but I can't figure it out at all.
Don't use a regex if you're trying to evaluate structured data. Regexes are not a magic wand you wave at every problem that happens to involve strings. What if you have a URL like http://www.some-other-domain.com/blah/blah/?www.example.com?
If you're trying to match a domain name to a domain name, then break apart the URL to get the host and compare that. In PHP, use the parse_url function. That will give you www.example.com as the host name, and then you can compare that to make sure it is the hostname you expect.
Try this
function DomainUrl($x) {
$url = $x;
if ( substr($url, 0, 7) == 'http://') { $url = substr($url, 7); }
if ( substr($url, 0, 8) == 'https://') { $url = substr($url, 8); }
if ( substr($url, 0, 4) == 'www.') { $url = substr($url, 4); }
if ( substr($url, 0, 4) == 'www9.') { $url = substr($url, 4); }
if ( strpos($url, '/') !== false) {
$ex = explode('/', $url);
$url = $ex['0'];
}
return $url;
}
$first = DomainUrl('http://www.example.com');
$second = DomainUrl('www.example.com/');
if($first == $second){
echo 'Match';
}else{
echo 'Not Match';
}
I am using file_get_content($url) which does not work with url starting from www.
So I am trying to append the http:// and converting into proper form if user entered url is not in correct form.
Check DEMO HERE
<?php
$url= 'www.google.com';
$pad = 'http://';
$cmp = 'www';
$prefix = substr($url , 0,2);
if($cmp == $prefix)
{
echo str_pad($url, strlen($url)+3 ,"$pad",STR_PAD_LEFT);
}
?>
This code does not echo correct url. Any issue here?
Why not use parse_url to figure it out?
$url = "www.example.com/test.php";
$parsedUrl = parse_url($url);
if(!array_key_exists('scheme', $parsedUrl)){
$url = "http://" . $url;
}
echo $url;
codepad example.
This is all you need:
if (strpos($url, '://') === false)
$url = 'http://' . $url;
check this
$url= 'www.google.com';
$pad = 'http://';
$cmp = 'www';
$prefix = substr($url , 0,3);
if($cmp == $prefix)
{
echo str_pad($url, strlen($url)+7 ,"$pad",STR_PAD_LEFT);
}
This is the code I currently have. How would I tweak it to include http:// in the href in the returned result every time? Currently, http:// is not in the returned result unless it's in the original string variable $text. I wish to have it added to the href if it is not in the original $text. Thanks!
function urlfixer($text){
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
$text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$text = preg_replace("/^www./", "", $text);
$last = -(strlen(strrchr($text, "/"))) + 1;
if ($last < 0) {
$text = substr($text, 0, $last) . "…";
}
return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
');
return preg_replace_callback($pattern, $callback, $text);
}
Since you don't know if your $url has http:// on it or not just stick it on the beginning, and then make sure it's stripped just in case.
$url = 'http://' . str_replace('http://','',$url);
return sprintf('<a rel="nofollow" href="%s">%s</a>', $url, $text);
$url = array_shift($matches);
if( substr($url,0,6)!='http://' ) {
$url='http://'.$url;
}
something like this should do it
I'm grabbing links from a website, but I'm having a problem in which the higher I set the recursion depth for the function the results become stranger
for example
when I set the function to the following
crawl_page("http://www.mangastream.com/", 10);
I will get a results like this for about half the page
http://mangastream.com/read/naruto/51619850/1/read/naruto/51619850/2/read/naruto/51619850/2/read/naruto/51619850/2/read/naruto/51619850/2/read/naruto/51619850/2/read/naruto/51619850/2/read/naruto/51619850/2
EDIT
while I'm expecting results like this instead
http://mangastream.com/manga/read/naruto/51619850/1
here's the function I've been using to get the results
function crawl_page($url, $depth)
{
static $seen = array();
if (isset($seen[$url]) || $depth === 0) {
return;
}
$seen[$url] = true;
$dom = new DOMDocument('1.0');
#$dom->loadHTMLFile($url);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$href = $element->getAttribute('href');
if (0 !== strpos($href, 'http')) {
$href = rtrim($url, '/') . '/' . ltrim($href, '/');
}
if(shouldScrape($href)==true)
crawl_page($href, $depth - 1);
}
echo $url,"\r";
//,pageStatus($url)
}
any help with this would be greatly appreciated
the construction of your new url is not correct, replace :
$href = rtrim($url, '/') . '/' . ltrim($href, '/');
with :
if (substr($href, 0, 1)=='/') {
// href relative to root
$info = parse_url($url);
$href = $info['scheme'].'//'.$info['host'].$href;
} else {
// href relative to current path
$href = rtrim(dirname($url), '/') . '/' . $href;
}
I think your problem lies in this line:
$href = rtrim($url, '/') . '/' . ltrim($href, '/');
To all relative urls on any given page this statement will prepend the current page url, which is obviously not what you want. What you need is to prepend only the protocol and host part of the URL.
Something like this should fix your problem (untested):
$url_parts = parse_url($url);
$href = $url_parts['scheme'] . '://' . $url_parts['host '] . $href;