how to strip out the http"// in php? - php

supposed a variable named $url,whcih value maybe $url=http://www.example.com or $url=http://example.com or $url=www.example.com
now, i want to echo the $url in www.example.com style, how to does this? thank you.

$url="http://www.example.com";
$data = parse_url($url);
echo $data["host"];
PHP parse_url: http://php.net/manual/en/function.parse-url.php

use str_replace function
echo str_replace('http://', '' , 'http://www.example.com');
$url = str_replace('http://', '' , 'http://www.example.com');

You can use strpos and str_replace to do the job. Search the http in the url through strpos if found replace with blank.
if (strpos($url,'http://') !== false)
{
$url=str_replace('http://','',$url);
}

Related

How can I replace %20 with - from url using php?

code:
<?php
$query = "select * from latest_news limit 0,10";
$fet = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($fet))
{
?>
<p id="news-h3"><?php echo $fetch['news_title']; ?>...[Read More]</p>
<?php
}
?>
When I click on link i.e.
[Read More]
It show
best%20engineering%20college%20in%20India
Here, I want to replace %20 with (-) from url and get result like
best-engineering-college-in-India
how can I fix this problem please help.
Thank you
1.you can use this php built in function.you just want to replace a piece of string by another.
$new_url = str_replace('%20', '-', $url);
for more information
http://fr2.php.net/str_replace
2.Use urlencode to encode. The decoding will happen automatically,
and if you want to remove total 20% then use this code.
$url = "one%20%26%20two";
$a = urldecode($url); // -> "one & two"
suppose your page when loading then create your url like this
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$base_url = $protocol . $_SERVER["SERVER_NAME"];
then use it
urldecode($base_url)
if you want to use this in url then
echo '<a href="?mycgi?foo=', urlencode($userinput), '">';
for more information
http://www.php.net/manual/en/function.urldecode.php
You should use str_replace() here:
echo str_replace('%20', '-', $input); // best-engineering-college-in-India
Basically you want to decode the URL. You can decode it using
echo utf8_decode(urldecode($input));

remove http and https from string in php

I have several urls like,
https://example.com/
http://example.com/
I only want "example.com" as string
And I want to remove the
https:// and http://
So I have taken array like this,
$removeChar = ["https://", "http://", "/"];
What is the proper way to remove these?
This worked for me,
$http_referer = str_replace($removeChar, "", "https://example.com/");
Use this php function.
Link : http://php.net/manual/en/function.parse-url.php (parse_url php function)
$url = "http://example.com/";
$domain = parse_url($url, PHP_URL_HOST);
get a result example.com
there is builtin function :
$domain = parse_url($url, PHP_URL_HOST);
try this:
$string = url ... ( your url);
$removeChar= array("http://","https://","/");
foreach($char in $removeChar)
{
$string= str_replace($char,"",$string);
}

How to get a specific value from URL in PHP

I have a URL:
market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001
How can I get this value from above URL 000146132647632302db63d958690001
Can I use preg_match function or something else.
If you are receiving it as real URL than as simple as:
echo $_GET['trackingid'];
Else:
$queryArray = [];
$query = parse_url(
urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001"),
PHP_URL_QUERY
);
parse_str($query, $queryArray);
echo $queryArray['trackingid'];
Live example
You can use regular expressions for that. Otherwise you cann access it direcly with $_GET
$url='market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001';
if(preg_match("/([^\?]*)\?trackingid%(d*)/",$url,$matches)){
echo $matches[1];
} else {
$_GET['trackingid']
}
1) One method is using explode().
$test = "market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001";
$url = explode("trackingid=",urldecode($test));
echo $url[1];
Working Demo : Click Here
2) Another is you can use preg_match() you can achieve it.
3) If you are getting it in url then get it using $_GET['trackingid'].
4) Using parse_str().
$url = urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001");
parse_str($url, $tempArray);
echo $tempArray['trackingid'];

PHP - Best way to find URL root from string?

I use PHP.
I have an URL that looks like this
http://www.mydomain.com/mydir/mydir2/?something=hello
I want this:
http://www.mydomain.com
I did it like this but it feels like the wrong way to do it
To long and ugly.
$url = 'http://www.mydomain.com/mydir/mydir2/?something=hello';
$root_url_a = explode('/', $url);
$root_url = $root_url_a[0] . '//' . $root_url_a[2];
$root_url_clean = $root_url_a[2];
Suggestions
Regex?
Xpath?
Some for me unknown PHP function?
The shortest most correct way of doing it will get my vote.
Ok here is an example:
$url = parse_url('http://www.mydomain.com/mydir/mydir2/?something=hello');
echo $url->scheme.'://'.$url->host;
Is along the right lines.
Though technically this is not even right since depending on whether you send in a scheme or not for a url parse_url can actually change the way it assigns variables, so I wrote:
function return_url($url){
$parsed_url = parse_url($url);
if(!$parsed_url){
return false;
}
if(isset($parsed_url['scheme'])){
if(!isset($parsed_url['host'])){
return false;
}else{
return $parsed_url['scheme'].'://'.$parsed_url['host'];
}
}
if(isset($parsed_url['path'])){
return 'http://'.$parsed_url['path'];
}
return false;
}
I would do it like this:
$url = "http://www.mydomain.com/mydir/mydir2/?something=hello";
echo parse_url($url, PHP_URL_HOST);
// Would echo:
http://www.mydomain.com
I would say RTM ;)
http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_HOST);
?>

Replacing a specific part of a query string PHP

I use $_SERVER['QUERY_STRING'] to get the query sting.
A example would be a=123&b=456&c=789
How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.
Any ideas appreciated, thanks.
A solution using url parsing:
parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
The value is going to be $_GET['b'].
How about:
str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);
you can use this function:
function Remove_QS_Key($url, $key) {
$url = preg_replace('/(?:&|(\?))'.$key.'=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}
to remove any key you want, e.g.
echo Remove_QS_Key("http://domain.com/?a=b&ref=dusername&c=d&e=f&g=h", "ref");
result
http://www.domain.com/?a=b&c=d&e=f&g=h
Try this:
$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);
All the answers look good, but it will be more flexible if you do:
// Make a copy of $_GET to keep the original data
$getCopy = $_GET;
unset($getCopy['b']); // or whatever var you want to take out
// This is your cleaned array
var_dump($getCopy);
// If you need the URL-encoded string, just use http_build_query()
$encodedString = http_build_query($getCopy);
You simply make a variable using $_GET and exclude b query string in build process:
$query_string_new = 'a=' . urlencode($_GET['a']) . '&c=' . urlencode($_GET['c']);
The $query_string_new should now contain a=123&c=789
Pear already has a class(Net_URL2) that handles URL parsing/building:
Install via Composer: https://packagist.org/packages/pear/net_url2
Install as include: https://github.com/pear/Net_URL2/blob/master/Net/URL2.php
Example code:
$url = new Net_URL2('http://www.example.com/?one=1');
$url->setQueryVariable('two', 2);
echo $url; // http://www.example.com/?one=1&two=2
Here is a function to replace a query parameter: (like example.com?a=1&b=2 -> example.com?a=5&b=2)
function replace_qs_key($key, $value) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$current_url_without_qs = strtok($current_url, '?');
parse_str($_SERVER['QUERY_STRING'], $query_params);
$query_params['page'] = $value;
$_SERVER['QUERY_STRING'] = http_build_query($query_params);
$new_url = $current_url_without_qs .'?'. $_SERVER['QUERY_STRING'];
return $new_url;
}

Categories