I have a link like below:
$link = http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3
How can i get it to be like the one below in PHP:
$link = http://mytour.com:8080
Sorry if this is a newbie question. Thanks.
parse_url($link)
$link = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parsed_link = parse_url($link);
echo $parsed_link['scheme']."//".$parsed_link['host'].":".$parsed_link['port'];
preg_match() - worse
preg_match("/^http[s]*:\/\/.*:\d+/", $link, $match);
echo $match[0];
You can use parse_url() to get url you want
$url = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parse_url = parse_url($url);
echo $link = $parse_url['scheme']."://".$parse_url['host'].":".$parse_url['port'];
echo "<br>";
//You can also use default method and params
echo $link = parse_url($url, PHP_URL_SCHEME)."//".parse_url($url, PHP_URL_HOST).":".parse_url($url, PHP_URL_PORT);
As suggested by #vladkras use parse_url($link)
$url = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parse = parse_url($url);
echo $parse['scheme'].'://'.$parse['host'].':'.$parse['port'];
Output:
http://mytour.com:8080
$link = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$url = parse_url($link);
echo $url['scheme'].'://'.$url['host'].':'.$url['port'];
Related
I have below code:-
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about
I want URL element that is - http://www.example.com/about
You can use PHP's inbuilt parse_url and parse_str functions.
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
parse_str(parse_url($link,PHP_URL_QUERY),$url);
echo $url['url'];
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$url = explode("&url=", $link)[1];
echo $url; //http://www.example.com/about
Try this:
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$uri=explode("url=",$link);
$uri=$uri[count($uri)-1];
echo $uri;
function getSomething($url) {
if (!(strpos($url, 'url=') !== false)) return false;
$parse = explode('url=', $url);
$code = $parse[1];
return $code;
}
echo getSomething("http://www.domain.com/go.php?id=545&url=http://www.example.com/about");
this will work even if url is placed somewhere else as parameter
$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
$url = "";
$params = explode("&", $link);
for($params as $param){
if(substr($param, 0, 4) === 'url='){
$url = explode("=", $param)[1];
break;
}
}
echo $url; //http://www.example.com/about
My source string could be:
example.com or http://example.com or www.example.com or https://example.com or http://www.example.com or https://www.example.com
or
example.abc.com or http://example.abc.com or www.example.abc.com or https://example.abc.com or http://www.example.abc.com or https://www.example.abc.com
I want the result: example
How can we do this using php string functions? or in other way?
Try this
$str = 'http://example.abc.com';
$last = explode("/", $str, 3);
$ans = explode('.',$last[2]);
echo $ans[0];
You can use parse_url
<?php
// Real full current URL, this can be useful for a lot of things
$url = 'http'.((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// Or you can put another url
$url = 'https://www.example.foo.biz/';
// Get the host name
$hostName = parse_url($url, PHP_URL_HOST);
// Get the first part of the host name
$host = substr($hostName, 0, strpos($hostName, '.'));
print_r($url);
print_r($hostName);
// Here is what you want
print_r($host);
?>
you can use strpos:
<?php
$url = "http://www.example.com";
/* Use any of you want.
$url = "https://example.com";
$url = "https://www.example.abc.com";
$url = "https://www.www.example.com"; */
if ($found = strpos($url,'example') !== false) {
echo "it exists";
}
?>
EDIT:
So this is what I cam up with now, using explode and substr:
$url = "http://www.example.com";
/* Use any of you want.
$url = "https://example.com";
$url = "https://www.example.abc.com";
$url = "https://www.www.example.com"; */
$exp ='example';
if ($found = strpos($url, $exp) !== false) {
echo $str = substr($url, strpos($url, $exp));
echo "<br>". "it exists" . "<br>";
$finalword = explode(".", $str);
var_dump($finalword);
}
?>
I get the following result :http://www.example.com/Music/\2011 Hits\Balle Lakka.mp3
Following is str_replace to try and replace local path URL to http:// path:
#mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT songlist.* FROM songlist WHERE ID='".$song->ID."'";
$result = mysql_query($query,$conn);
$i=0;
while($data = mysql_fetch_assoc($result))
{
while(list($key,$value) = each($data))
$arr[$i][$key] = $value;
$i++;
}
$file = str_replace("C:\inetpub\Music","http://www.example.com/Music/",$arr[0]['filename']);
$file = str_replace('\\','/',$file);
add it to the end. It should work..
Can you please try this,
Simply:
echo $url = str_replace("/\\","/",$url);
another method,
$url = str_replace("C:\inetpub\Music","http://www.mydomain.com/Music/","http://www.mydomain.com/Music/2011 Hits/\Balle Lakka.mp3");
$url = str_replace("\\","/",$url);
$url = str_replace("//","/",$url);
echo $url = str_replace("http:/","http://",$url);
in while loop you can do following:
while(list($key,$value) = each($data))
$arr[$i][$key] = ($key!='filename') ? $value : str_replace('\\','/', ltrim($value, '\\'));
Try something like this
<?php
$str='http://www.example.com/Music/\2011 Hits\Balle Lakka.mp3';
$str=str_replace(array('\\','//',':'),'/',$str);
echo $str;
OUTPUT:
http://www.example.com/Music/2011 Hits/Balle Lakka.mp3
To get the domain name i am using this code:
<?php
$myURL = 'http://answers.yahoo.com/question/index?qid=20130406061745AAmovgl';
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
if (preg_match($pattern, $myURL, $domain) === 1) {
$domain = $domain[0];
}
$ndomain = "http://$domain";
echo $ndomain;
?>
but it will output: http://yahoo.comBut, how i can output http://answers.yahoo.com this sub-domain exactly.
You should instead use the parse_url function, since it exists to do this very thing.
echo parse_url( $url, PHP_URL_HOST );
You can use parse_url()like this:
$urlData = parse_url($myURL);
$host = $urlData['host']; //domain + subdomain
I have an URL say: www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go. I want to extract the string register-car from it using php code. Please help.
try this:
<?php
$url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";
$register_car = basename(parse_url($url, PHP_URL_PATH));
echo $register_car; // will echo "register-car"
?>
You can use a combination of parse_url and basename, or even pathinfo to help you with this.
In PHP < 5.4.7
$url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";
$result = basename(parse_url("http://" . $url, PHP_URL_PATH));
echo $result; // register-car
In PHP >= 5.4.7
$url = "www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go";
$result = basename(parse_url("//" . $url, PHP_URL_PATH));
echo $result; // register-car
You are looking for the parse_url function, followed by basename to return the last /item/. The following prints register-car.
$parsed = parse_url("http://www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go", PHP_URL_HOST);
echo basename($parsed['path']);
$parts = parse_url("www.abc.com/blog/2012/12/register-car?w=Search&searchDmv=Go", PHP_URL_PATH);
$pieces = explode($parts, "/");
$action = $pieces[count($pieces)-1];