How to trim my home url on the localhost - php

This is how I get my url on my localhost:
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
echo $url;
it returns : http://localhost/CodeSensei/menu because I am on the menu page.
How can I trim this? I only want "http://localhost/CodeSensei"
I know I can trim it like this
echo trim($url,"menu");
But the problem is, that "menu" is dynamic, it keep changing dependes on the page. Is there any way to trim my url so it will always and only print "http://localhost/CodeSensei" in any page?

There are many ways to achieve this. You can play around with different string manipulators like explode() etc.
Here is a solution using explode()
$variable = "http://localhost/CodeSensei/menu";
$variable = (explode("/",$variable));
$url='';
for($i=2;$i<count($variable)-1;$i++)
{
$url .= "/".$variable[$i];
}
$final_url = "http:/".$url;
echo $final_url;
Your output
http://localhost/CodeSensei

This function may help you. It get a url and return the url without after the last /.
<?php
function getSubUrl($originUrl) {
$url = parse_url($originUrl);
$url['scheme'] .= '://';
$url['path'] = dirname($url['path']);
return implode($url);
}
echo getSubUrl('http://localhost/CodeSensei/menu/123') . PHP_EOL;
// http://localhost/CodeSensei/menu
echo getSubUrl('http://localhost/CodeSensei/menu') . PHP_EOL;
// http://localhost/CodeSensei
echo getSubUrl('http://localhost/CodeSensei') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost/') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost') . PHP_EOL;
// http://localhost

Related

convert url to hyperlink on text as formatted

I am using following code for convert url to hyperlink on text.But the problem is i want to use shorten title for hyperlink for example this is url http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted and after convert like this :
http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted
I want to this :
http://stackoverflow.com/...
This is my code :
$stringdata = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 $2', $stringdata);
Title should be shorten but url should be same of original.
Thankyou.
You could always use parse_url found here
Here's an example:
$url = 'http://www.stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$splitUrl = parse_url($url);
echo '<a href="' . $url . '"/>' . $splitUrl['scheme'] . '://' . $splitUrl['host'] .'/... </a>';
parse_urlcreates an array from the URL provided.
UPDATE:
Using Mikael Roos answer at that link I came up with what you needed it to do.
function make_clickable($text) {
$regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
return preg_replace_callback($regex, function ($matches) {
$splitUrl = parse_url($matches[0]);
return "<a href='{$matches[0]}'>{$splitUrl['scheme']}://{$splitUrl['host']}/..</a>";
}, $text);
}
echo make_clickable('Some odd text here that makes https://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted clickable');
try this
$var='http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$array=explode('/', $var);
$url=$array[0]."//".$array[2]."/...";

Internal Server Error when trying to check url

I'm trying to check the string after the last trailing slash in my URL.
My code is as follows:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = substr($url, strrpos($url, '/') + 1);
if($data == "dashboard") {
require_once VIEW_ROOT . '/cp/dashboard_view.php';
} else {
echo $data;
}
Once I go to http://MYURL/dashboard/in it should show in as the $data. Instead it gives me a 500 error.
You can simply use explode() function to break the string... .Or else $_SERVER[REQUEST_URI] shall give you the data after the host name...
But for the data after the last '/' explode function will work the best..
This will work.
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$x = explode('/',$url);
$data = $x[sizeof($x)-1];
echo $data;
You should try :
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
You need to join
http:// string with $_SERVER[HTTP_HOST] and then $_SERVER[REQUEST_URI] using .(dot).

PHP find base url

I have the following code
Donate Now
What I want to do with php is replace the absolute url part with php then just append biography/#donate
so basically generating the url so whatever page the user is on they get sent to www.mysite.com/biography/#donate
I have tried using $_SERVER['DOCUMENT_ROOT'] but just seems to add the link to the existing url
thanks in advance
In such cases i'm using the <base> tag in header.
<base href="http://myabsolute.host/or.subfolder/" />
Then i just use relative links to my anchors, which are resolved to my base url.
I've used parse_url in my example. I hope this is what you're looking for:
<?php
$url = "http://blown-away.org/biography/#donate";
$mySiteHost = "mysite.com";
$parts = parse_url($url);
echo str_replace($parts["host"], $mySiteHost, $url)
?>
$url = 'http://blown-away.org/biography/#donate';
echo "Original Url : " . $url . "</br></br>";
$tempArray = parse_url($url);
$tempArray['host'] = "www.mysite.com";
$tempUrlString = "";
foreach($tempArray as $key => $value){
if($key == "scheme"){
$tempUrlString = $tempUrlString . $value . "://";
}else{
$tempUrlString = $tempUrlString . $value;
}
}
echo "Modified Url : " . $tempUrlString . "";
You can use $_SERVER["SERVER_NAME"]; instead

How to remove part of the url ratther than the shop url from the currently fetched web page url

In my prestashop shop i have fetched the current web page url by using the below php code.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
My current echo url is http://shoppingworld.com/int/Mens-Tshirts/Fashion.html
My shop url is http://shoppingworld.com/int/
I need to remove the url portion which is coming next to the above shop url.
Try this
<?php
$url_path="http://www.shoppingworld.com/int/Mens-Tshirts/Fashion.html";
$a = parse_url($url_path, PHP_URL_SCHEME);
$b = parse_url($url_path, PHP_URL_HOST);
$url_name_parse=explode('/',$url_path);
$url_name=$url_name_parse[3];
echo ($a . "://" . $b .'/' .$url_name.'/'); ?>
Program Output
http://www.shoppingworld.com/int/
DEMO
You can't directly get that partial url.
Try this,
$url = 'http://shoppingworld.com/int/Mens-Tshirts/Fashion.html';
$parsed = parse_url($url);
$path_array = explode('/', $parsed['path']);
echo $parsed['scheme'] . '//' . $parsed['host'] .'/'. $path_array[1] . '/';
Demo
$arr_url = parse_url($url);
$host = $arr_url['host'];
$service_uri = $arr_url['path'];
read more in php manual about parse_url();

Remove Subdomain from URL string , keeping the rest of URL (for replacing subdomain)

i have a location menu that has to change location, the good thing is every url exist in every city,, and every city is a subdomain
city1.domain.com.uk/index.php?page=category/238/12
city2.domain.com.uk/index.php?page=category/238/12
Im trying this. Im trying to break the URL to remove subdomain , so i can replace it for each item in menu
I want to get index.php?page=category/238/12
<?PHP
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
// break it up using the "."
$urlb = explode('.',$url);
// get the domain
$dns = $urlb[count($urlb)-1];
// get the extension
$ext = $urlb[count($urlb)+0];
//put it back together
$fullDomain = $dns.'.'.$ext;
echo $fullDomain;
?>
But i Get this php?page=category/238/12
Also i havent think in a solution for an issue i will be facing with this..
If im looking at a product the url change to something like
city2.domain.com.uk/index.php?page=item/preview/25
But, the products dont exist in every city , so my user will get a 404.
=(
How can i make a conditional in the process so if page=item/preview/25 i do replace this for
page=index/index
You can split the domain as:
$url = "city1.domain.com.uk/index.php?page=category/238/12";
list($subDomain, $params) = explode('?', $url);
list($domain, $sub) = explode('/', $subDomain);
$newUrl = $sub . "?" . $params;
echo $newUrl;
Cheers!
How about this:
<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
$url=(parse_url($url));
$dns = substr($url['host'],stripos($url['host'],'.')+1);
$fullDomain =$url['scheme']."://".$dns.$url['path']."?".$url['query'].$url['fragment'];
if (substr($url['query'],stripos($url['query'],'=')+1,stripos($url['query'],'/')-stripos($url['query'],'=')-1)=='item') {
echo "redirect";
} else {
echo "don't redirect";
}
echo "<br>".$fullDomain;
?>

Categories