So what I have been trying to do is, checking some URLS to see if they have a specific TLD in them. For example:
**
if foo.edu has .edu in it or not.
**
For this I have a table in my Database with some TLDs added in it such has .gov,.edu etc
What I have been trying to do is create a function which compares the given URL with strings(TLDS) in the database. What I am encountering is kind of strange or maybe I'm just exhausted that I cannot think of anything else. The function should just tell me if it did found any such string(HERE:TLD) in the url but case is different:
strpos_array() is a function I am using for taking arrays in strpos();
Code :
public function get_specialTLD(){
$query = $this->db->select('TLD');
$query = $this->db->from('foo_tlds');
$query = $this->db->get();
return $query->result_array(); //outputs the list of TLDS in the table
}
Should check the URL against each TLD
public function specialCheck($site){
$tlds = $this->get_specialTLD();
foreach($tlds as $tld){
$exploded = explode(' ',$tld['TLD']);
if($this->strpos_array($site, $exploded)>0){
echo 'spl';
}else{
echo 'no';
}
}
}
Now in this case what this will output is
no no spl(if this index(example:.edu) of TLD was found matching). If I instead try returning boolean instead of echoing, it will obviously just stop at the first instance of the check, that is if it found no matches, it will return FALSE and for the rest of TLDs will be left without checking with the URL.
What I am suspecting is this is because of just using foreach(), maybe I should be using something else.? or I should stick to hard coded method which uses strstr
if ((strstr($site,".gov")) or (strstr($site,".edu")) or (strstr($site,".net"))) )
{
echo 'Spl'
}
Why not do it like this?
public function specialCheck($site){
$tlds = $this->get_specialTLD(); foreach($tlds as $tld){ $exploded = explode(' ',$tld['TLD']); if($this->strpos_array($site, $exploded)>0){ return TRUE; } } return FALSE;
}
In this case, if it finds a match it'll return TRUE otherwise FALSE.
It won't exit until a match has been found or until all TLDs have been checked.