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);
?>
Related
I am going to make a URL checking system.
I have this URL
https://lasvegas.craigslist.org/mob/6169799901.html
Now I want to make this URL like this
https://lasvegas.craigslist.org/search/mob?query=6169799901
how can I do it using PHP?
Since I ended up (maybe?) solving it anyways, here's one method using URL/path parsing:
$url = 'https://lasvegas.craigslist.org/mob/6169799901.html';
$parsed = parse_url($url);
$basepath = pathinfo($parsed['path']);
echo $parsed['scheme'].
"://".
$parsed['host'].
"/search".
$basepath['dirname'].
"?query=".
$basepath['filename'];
Formatted for readability.
https://3v4l.org/E6Y54
Try this
$url = "https://lasvegas.craigslist.org/mob/6169799901.html";
$id = substr($url, strrpos($url, '/') + 1);
$id = str_replace(".html","",$id);
$result = "https://lasvegas.craigslist.org/search/mob?query=".$id;
echo $result;
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);
}
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'];
How can i get a certain part of a URL i have searched google and read a few tutorials but can't seem to get my head around it maybe someone can show me from the example below.
Here is my code
<?php
include "simple_html_dom.php";
$title = "fast";
$html = file_get_html("http://www.imdb.com/find?q=".urlencode($title)."&s=all");
$element = $html->find('[class="result_text"] a', 1);
$link = $element->href;
echo $link;
// Clear dom object
$html->clear();
unset($html);
?>
Now this echos
/title/tt0109772/?ref_=fn_al_tt_2
But i only want the imdb id.
tt0109772
So can someone explain or show me how to do this please
thanks
If it's always the subdir:
echo basename(dirname($link));
you can Use explode():
$str = "/title/tt0109772/?ref_=fn_al_tt_2";
$arrUrl = explode("/",$str);
$id = $arrUrl[2];
echo $id;
demo
You can use explode like:
$parts = explode("/", $url);
This will split URL to array. Then check what index you want.. and get it like:
$id = $parts[3];
You can debug with: print_r($parts);
Not the most elegant way, but
$link = '/title/tt0109772/?ref_=fn_al_tt_2';
$imdb_id = explode('/', $link);
$imdb_id = $imdb_id[2];
echo $imdb_id;
Will work.
Try this code: (source)
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);
Then I think you need $segments[1] to get the IMDB id.
If I have the url mysite.com/test.php?id=1. The id is set when the page loads and can be anything. There could also be others in there such as ?id=1&sort=new. Is there a way just to add another to the end without finding out what the others are first then building a new url? thanks.
As an alternative to Kolink's answer, I think I would utilize http_build_query(). This way, if there is nothing in the query string, you don't get an extra &. Although, it won't really make a difference at all. Kolink's answer is perfectly fine. I'm posting this mainly to introduce you to http_build_query(), as you will likely need it later:
http_build_query(array_merge($_GET, array('newvar'=>'123')))
Basically, we use http_build_query() to take everything in $_GET, and merge it with an array of any other parameters we want. In this example, I just create an array on the fly, using your example parameter. In practice, you'll likely have an array like this somewhere already.
"?".$_SERVER['QUERY_STRING']."&newvar=123";
Something like that.
Use this function: https://github.com/patrykparcheta/misc/blob/master/addQueryArgs.php
function addQueryArgs(array $args, string $url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$urlParts = parse_url($url);
if (isset($urlParts['query'])) {
parse_str($urlParts['query'], $urlQueryArgs);
$urlParts['query'] = http_build_query(array_merge($urlQueryArgs, $args));
$newUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query'];
} else {
$newUrl = $url . '?' . http_build_query($args);
}
return $newUrl;
} else {
return $url;
}
}
$newUrl = addQueryArgs(array('add' => 'this', 'and' => 'this'), 'http://example.com/?have=others');