Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用有序数组_Php - Fatal编程技术网

如何在php中使用有序数组

如何在php中使用有序数组,php,Php,我想从链接中提取网站名称,因此我编写了以下函数: protected function getWebsiteName() { $prefixs = ['https://', 'http://', 'www.']; foreach($prefixs as $prefix) { if(strpos($this->website_link, $prefix) !== false) { $len = strlen($

我想从链接中提取网站名称,因此我编写了以下函数:

protected function getWebsiteName()
{
    $prefixs = ['https://', 'http://', 'www.'];

    foreach($prefixs as $prefix)
    {
        if(strpos($this->website_link, $prefix) !== false)
        {
            $len = strlen($prefix);
            $this->website_name = substr($this->website_link, $len);
            $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
        }
    }
}
问题是,当我使用类似于的I网站链接时,结果是:s://www,而该函数仅在我从数组列表中删除该“www.”时才起作用


你知道为什么会发生这种情况,或者我如何改进这个函数吗?

你可以使用
parse_url(),请尝试:

print_r(parse_url('https//www.name/'));

您可以使用
parse_url(),请尝试:

print_r(parse_url('https//www.name/'));

您可以使用
parse_url(),请尝试:

print_r(parse_url('https//www.name/'));

您可以使用
parse_url(),请尝试:

print_r(parse_url('https//www.name/'));

让我们看看你的代码。每次通过
foreach
您都在应用原始
网站链接中的逻辑。这意味着,在前两次迭代之后,在
www.
的情况下运行
strlen
时,会发生以下情况:

  • $prefix
    www.
  • 因此,
    $len=4
    $prefix
    的长度)
  • $this->website\u链接仍然是
    https://www.github.com
  • 您可以应用
    substr($this->website\u link,4)
  • 结果是
    $this->website\u name='s://www.github.com'
  • 您应用
    substr($this->website\u name,0,7)
    7
    strpos($this->website\u name,“.”)的结果)
  • 结果是
    $this->website\u name='s://www'
  • 要解决此问题,您应该将
    $this->website\u link
    保存到
    $temp
    ,然后使用以下代码:

    $temp = $this->website_link;
    foreach($prefixs as $prefix)
    {
        if(strpos($temp, $prefix) !== false)
        {
            $len = strlen($prefix);
            $temp = substr($temp, $len);
        }
    }
    $this->website_name = substr($temp, 0, strpos($temp, '.'));
    
    我建议使用@dynamic的答案,但如果您想继续使用字符串替换策略,请使用
    str\u replace
    。它接受针的数组

    $prefixes = ['https://', 'http://', 'www.'];
    $this->website_name = str_replace($prefixes, '', $this->website_link);
    $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
    

    让我们看一下您的代码。每次通过
    foreach
    您都在应用原始
    网站链接中的逻辑。这意味着在前两次迭代后,在
    www.
    的情况下运行
    strlen
    时,会发生以下情况:

  • $prefix
    www.
  • 因此,
    $len=4
    $prefix
    的长度)
  • $this->website\u链接仍然是
    https://www.github.com
  • 您可以应用
    substr($this->website\u link,4)
  • 结果是
    $this->website\u name='s://www.github.com'
  • 您应用
    substr($this->website\u name,0,7)
    7
    strpos($this->website\u name,“.”)的结果)
  • 结果是
    $this->website\u name='s://www'
  • 要解决此问题,您应该将
    $this->website\u link
    保存到
    $temp
    ,然后使用以下代码:

    $temp = $this->website_link;
    foreach($prefixs as $prefix)
    {
        if(strpos($temp, $prefix) !== false)
        {
            $len = strlen($prefix);
            $temp = substr($temp, $len);
        }
    }
    $this->website_name = substr($temp, 0, strpos($temp, '.'));
    
    我建议使用@dynamic的答案,但如果您想继续使用字符串替换策略,请使用
    str\u replace
    。它接受针的数组

    $prefixes = ['https://', 'http://', 'www.'];
    $this->website_name = str_replace($prefixes, '', $this->website_link);
    $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
    

    让我们看一下您的代码。每次通过
    foreach
    您都在应用原始
    网站链接中的逻辑。这意味着在前两次迭代后,在
    www.
    的情况下运行
    strlen
    时,会发生以下情况:

  • $prefix
    www.
  • 因此,
    $len=4
    $prefix
    的长度)
  • $this->website\u链接仍然是
    https://www.github.com
  • 您可以应用
    substr($this->website\u link,4)
  • 结果是
    $this->website\u name='s://www.github.com'
  • 您应用
    substr($this->website\u name,0,7)
    7
    strpos($this->website\u name,“.”)的结果)
  • 结果是
    $this->website\u name='s://www'
  • 要解决此问题,您应该将
    $this->website\u link
    保存到
    $temp
    ,然后使用以下代码:

    $temp = $this->website_link;
    foreach($prefixs as $prefix)
    {
        if(strpos($temp, $prefix) !== false)
        {
            $len = strlen($prefix);
            $temp = substr($temp, $len);
        }
    }
    $this->website_name = substr($temp, 0, strpos($temp, '.'));
    
    我建议使用@dynamic的答案,但如果您想继续使用字符串替换策略,请使用
    str\u replace
    。它接受针的数组

    $prefixes = ['https://', 'http://', 'www.'];
    $this->website_name = str_replace($prefixes, '', $this->website_link);
    $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
    

    让我们看一下您的代码。每次通过
    foreach
    您都在应用原始
    网站链接中的逻辑。这意味着在前两次迭代后,在
    www.
    的情况下运行
    strlen
    时,会发生以下情况:

  • $prefix
    www.
  • 因此,
    $len=4
    $prefix
    的长度)
  • $this->website\u链接仍然是
    https://www.github.com
  • 您可以应用
    substr($this->website\u link,4)
  • 结果是
    $this->website\u name='s://www.github.com'
  • 您应用
    substr($this->website\u name,0,7)
    7
    strpos($this->website\u name,“.”)的结果)
  • 结果是
    $this->website\u name='s://www'
  • 要解决此问题,您应该将
    $this->website\u link
    保存到
    $temp
    ,然后使用以下代码:

    $temp = $this->website_link;
    foreach($prefixs as $prefix)
    {
        if(strpos($temp, $prefix) !== false)
        {
            $len = strlen($prefix);
            $temp = substr($temp, $len);
        }
    }
    $this->website_name = substr($temp, 0, strpos($temp, '.'));
    
    我建议使用@dynamic的答案,但如果您想继续使用字符串替换策略,请使用
    str\u replace
    。它接受针的数组

    $prefixes = ['https://', 'http://', 'www.'];
    $this->website_name = str_replace($prefixes, '', $this->website_link);
    $this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
    

    是的,使用parse_url和preg_match应该可以完成这项工作

    function getWebsiteName($url)
    {
      $pieces = parse_url($url);
      $domain = isset($pieces['host']) ? $pieces['host'] : '';
      if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
        return $regs['domain'];
      }
      return false;
    }
    

    是的,使用parse_url和preg_match应该可以完成这项工作

    function getWebsiteName($url)
    {
      $pieces = parse_url($url);
      $domain = isset($pieces['host']) ? $pieces['host'] : '';
      if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
        return $regs['domain'];
      }
      return false;
    }
    

    是的,使用parse_url和preg_match应该可以完成这项工作

    function getWebsiteName($url)
    {
      $pieces = parse_url($url);
      $domain = isset($pieces['host']) ? $pieces['host'] : '';
      if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
        return $regs['domain'];
      }
      return false;
    }
    

    是的,使用parse_url和preg_match应该可以完成这项工作

    function getWebsiteName($url)
    {
      $pieces = parse_url($url);
      $domain = isset($pieces['host']) ? $pieces['host'] : '';
      if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
        return $regs['domain'];
      }
      return false;
    }
    

    好的,我会尝试一下,但是关于上面的行为,我为什么会得到这个结果。@sjagr你的意思是什么?它正是我需要它做的!!从parse_url()的文档中,我仍然需要解析主机名(www.example.com)来提取(示例)好的,我会试试看,但是关于上面的行为,我为什么会得到这个结果。@sjagr你的意思是什么?它正是我需要它做的!!从parse_url()的文档中,我仍然需要