PHP Remove URL from array if not unique - php

I have got a large array of lets say 500 urls, now using array_unique I can remove any duplicate values. However I am wanting to remove any duplicate values where the domain is the same while keeping the original domain (so only removing duplicates so this value is now unique).
I have been using the following however this only removes duplicate values:
$directurls = array_unique($directurls);
I have been playing around with the following to get the domain but am wondering how I can check the entire array for other parse_url domains in the array:
foreach($directurls as $url) {
$parse = parse_url($url);
print $parse['host']; //the domain name I just need to find a way to check this and remove it
}
I imagine I will need to use some form of loop perhaps where I can get the current host and check all other hosts in the array. If duplicates remove all duplicates and keep the current value. Maybe something like this could work am just testing it now:
foreach($directurls as $url) {
$parse = parse_url($url);
if (in_array($parse['host'], $directurls)) {
//just looking for a way to remove while keeping unique
}
}
If anyone has any suggestions or recommendations on other ways to go about this I would be very thankful.
Let me know if I need to explain a bit more.

You could avoid having to loop through the URLs by using array_map() with a callback function.Grab the domain using parse_url(), and create a new array with just the domains. Now, you can simply create a new array with the URLs as keys and domains as values and just call array_unique() to get the unique items. Now, to get just the URLs into a new array, you can use array_keys():
$domains = array_map(function($d) {
$parts = parse_url($d); // or: parse_url($d)['host'] if PHP > 5.4
return $parts['host'];
}, $directurls);
$result = array_keys(array_unique(array_combine($directurls, $domains)));
Demo!

Related

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

How do I select a certain part of a string in PHP?

I have an array of URLs. The URLs look similar to:
https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=myAPIkey
The $place_id is a variable that changes in every single url.
I want to be able to select just this part of the url.
So when I loop through the URLs, I want it to extract that part, of the URL I am using, and assign it to a variable ($place) that I can use in my code. I have looked at strpos() and / or substr(), but I don't know how I would use them since the $place_id changes every time. I think the length of $place_id is always the same, but I am not 100% certain so a solution that worked for different lengths would be preferable.
Alternatively, I am already have the multiple $place_ids in an array. I am generating the array of URLs by using
foreach ($place_ids as $place_id) {
array_push(
$urls,
"https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=myAPIkey"
);
}
I am then using
foreach ($urls as $url){}
to loop through this array. If there was a way I could check to make sure the value of $place_id coincided with the value of the URL either through some sort of check or by using their position in the array that would work too.
I'm sorry if any of this is incorrect and if there are any questions I can answer or ways for me to improve my question I'd be happy to.
Since you're dealing with URLs here, you'll probably want to use PHP's built-in helpers, parse_url() and parse_str().
For example, you can separate the GET parameters with parse_url and then extract each argument into an array with parse_str:
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=<arg1>&key=<arg2>';
$args = [];
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $args);
// args => ["placeid" => "<arg1>", "key" => "<arg2>"]
A regular expression will do
$match = array();
$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=346758236546&key=myAPIkey";
preg_match('/[?&]placeid=([^&]+)(?:$|.*)/', $url, $match);
var_dump($match[1]); // string(12) "346758236546"

How get size_of $_GET

I want to get the number of values that were submitted via my $_GET. I'm going to be using a for loop and every 7 are going to be saved to a class. Does anyone know how to find the Size_of the $_GET array? I looked through this info $_GET Manual, but don't see what I'm looking for.
This is a code snippet that I started in the class that I go to after the submit.
for($i=0;$i<$_GET<size>;$i++) {
$ID = $_GET["ID"][$i];
$Desc = $_GET["Desc"][$i];
$Yevaluation = $_GET["Yevaluation"][$i];
$Mevaluation = $_GET["Mevaluation"][$i];
$Cevaluation = $_GET["Cevaluation"][$i];
$Kevaluation = $_GET["Kevaluation"][$i];
$comment = $_GET["comment"][$i];
//saving bunch to class next
}
$_GET is an array. Like any other array in PHP, to find the number of elements in an array you can use count() or its alias sizeof().
count($_GET["ID"])
could work. However, then you have to be sure that each index contains the same number of elements.
More secure would be
min(count($_GET["ID"]), count($_GET["Desc"]), ...)
With array_map this can probably be shortcut to something like:
min(array_map(count, $_GET))
Try thinking through the last one, if it seems difficult to you. It's called functional programming and it's an awesome way to shortcut some stuff. In fact array_map calls another function on each element of an array. It's actually the same as:
$filteredGet = array();
foreach ($element in $_GET) { // this is the array_map part
$filteredGet[] = count($element);
}
min($filteredGet);
Even better would be a structure where $i is the first level of the array, but I think this is not possible (built-in) with PHPs $_GET-parsing.

Get parts of URL in PHP

I am just wondering what the best way of extracting "parameters" from an URL would be, using PHP.
If I got the URL:
http://example.com/user/100
How can I get the user id (100) using PHP?
To be thorough, you'll want to start with parse_url().
$parts=parse_url("http://example.com/user/100");
That will give you an array with a handful of keys. The one you are looking for is path.
Split the path on / and take the last one.
$path_parts=explode('/', $parts['path']);
Your ID is now in $path_parts[count($path_parts)-1].
You can use parse_url(), i.e.:
$parts = parse_url("http://x.com/user/100");
$path_parts= explode('/', $parts[path]);
$user = $path_parts[2];
echo $user;
# 100
parse_url()
This function parses a URL and returns an associative array containing
any of the various components of the URL that are present. The values
of the array elements are not URL decoded.
This function is notmeant to validate the given URL, it only breaks it
up into the above listed parts. Partial URLs are also
accepted, parse_url() tries its best to parse them correctly.
$url = "http://example.com/user/100";
$parts = Explode('/', $url);
$id = $parts[count($parts) - 1];
I know this is an old thread but I think the following is a better answer:
basename(dirname(__FILE__))

PHP: Find common repeating string?

I have this situation in PHP. I have an array that has these keys for example, wires-1, wires-2, wires-3. I need a function or way for my program to read these keys, and find that the common word is wires? How would that be accomplished in PHP? Thanks for your help.
Take a look at how an autocomplete's functionality works, this is similar to your approach.
I'm sure there's plenty of source codes for autocomplete on google
For the string value of every key in your array:
Throw away all non-alpha characters, i.e. leave only letters such that ctype_alpha($remaining_text) should return true.
Keep an array with the found words as keys, and their frequencies as values, as such:
$array = new array();
function found_word($word)
{ global $array;
if(!isset($array[$word])) { $array[$word] = 1; }
else { $array[$word]++; }
}
Only nicer ;)
Sort the array in reverse by using arsort($array);
$array now contains the most found words as its first elements.
you would have to create every possible suffix of every string you have.
create a map for every suffix you found
count the occurence of every suffix in your string array
you can modify the performance with f.ex. limiting the suffix length

Categories