strpos() 循环中数据库中的字符串
strpos() with string from database in loop
所以我一直在尝试做的是,检查一些 URLS 以查看它们是否有特定的 TLD。例如:
**
if foo.edu has .edu in it or not.
**
为此,我的数据库中有一个 table,其中添加了一些 TLD,例如 .gov、.edu 等
我一直试图做的是创建一个函数,将给定的 URL 与数据库中的字符串 (TLDS) 进行比较。我遇到的事情有点奇怪,或者我可能只是精疲力尽,想不出其他事情。该函数应该只告诉我它是否在 url 中找到了任何这样的字符串(HERE:TLD),但大小写不同:
strpos_array() 是我用于在 strpos();
中获取数组的函数
代码:
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
}
应针对每个 TLD
检查 URL
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';
}
}
}
现在在这种情况下输出的是
no no spl(如果找到匹配的 TLD 索引(示例:.edu))。
如果我改为尝试 returning 布尔值而不是回显,它显然只会在检查的第一个实例处停止,即
如果没有找到匹配项,它将 return FALSE 并且其余的 TLD 将被保留而不用 URL.
检查
我怀疑这是因为只使用了 foreach(),也许我应该使用其他东西。?或者我应该坚持使用 strstr
的硬编码方法
if ((strstr($site,".gov")) or (strstr($site,".edu")) or (strstr($site,".net"))) )
{
echo 'Spl'
}
如果我正确理解你的问题,你需要break
:
$found = false;
foreach($tlds as $tld){
$exploded = explode(' ',$tld['TLD']);
if($this->strpos_array($site, $exploded)>0){
echo 'spl';
$found = true;
break;
}else{
echo 'no';
}
}
return $found;
为什么不这样做呢?
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;
}
在这种情况下,如果找到匹配项,它将 return 为真,否则为假。
在找到匹配项或检查完所有 TLD 之前,它不会退出。
所以我一直在尝试做的是,检查一些 URLS 以查看它们是否有特定的 TLD。例如:
**
if foo.edu has .edu in it or not.
**
为此,我的数据库中有一个 table,其中添加了一些 TLD,例如 .gov、.edu 等
我一直试图做的是创建一个函数,将给定的 URL 与数据库中的字符串 (TLDS) 进行比较。我遇到的事情有点奇怪,或者我可能只是精疲力尽,想不出其他事情。该函数应该只告诉我它是否在 url 中找到了任何这样的字符串(HERE:TLD),但大小写不同:
strpos_array() 是我用于在 strpos();
中获取数组的函数代码:
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
}
应针对每个 TLD
检查 URLpublic 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';
}
}
}
现在在这种情况下输出的是
no no spl(如果找到匹配的 TLD 索引(示例:.edu))。 如果我改为尝试 returning 布尔值而不是回显,它显然只会在检查的第一个实例处停止,即 如果没有找到匹配项,它将 return FALSE 并且其余的 TLD 将被保留而不用 URL.
检查我怀疑这是因为只使用了 foreach(),也许我应该使用其他东西。?或者我应该坚持使用 strstr
的硬编码方法if ((strstr($site,".gov")) or (strstr($site,".edu")) or (strstr($site,".net"))) )
{
echo 'Spl'
}
如果我正确理解你的问题,你需要break
:
$found = false;
foreach($tlds as $tld){
$exploded = explode(' ',$tld['TLD']);
if($this->strpos_array($site, $exploded)>0){
echo 'spl';
$found = true;
break;
}else{
echo 'no';
}
}
return $found;
为什么不这样做呢?
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;
}
在这种情况下,如果找到匹配项,它将 return 为真,否则为假。
在找到匹配项或检查完所有 TLD 之前,它不会退出。