【问题标题】:All array value keys at [0] with simple html dom[0] 处的所有数组值键,带有简单的 html dom
【发布时间】:2015-10-01 09:15:28
【问题描述】:

我正在尝试为动漫创建一个镜像网站

file_get_contents($page);

$html->load_file($page);

$links = array();

foreach($html->find('iframe') as $element) 
{
    $links[] = $element;    
}

foreach ($links as $out) 
{
$links = preg_match('/(http:\/\/mp4upload.com).+?(html)/', $out, $matches);
unset($matches[1]);
unset($matches[2]);
    if($matches){                               
        $mirror_link = $matches[0];

这是我当前的代码,但每次我加载它

数组是这样显示的

Array ( [0] => LINK ) 
Array ( [0] => LINK  ) 
Array ( [0] => LINK  ) 
Array ( [0] => LINK )

是否可以将其限制为仅 1 个链接并删除其余链接?

【问题讨论】:

  • 当你问一个问题,只得到+rep xD的那一刻
  • 停止使用这个糟糕的 API 并学习 DOMDocument 和 DOMXPath (它们能够更好更快地做同样的事情)

标签: php regex


【解决方案1】:

当你找到你要找的东西时,你必须break你的循环:

$mirror_link = [];

foreach ($links as $out) {
    ...

    if ($matches) {                               
        $mirror_link = $matches[0];

        break; // <-- Will prevent loop form iterating further.
        //break(2) will break current and parent loop
    }
}

【讨论】:

  • 是的,但我无法在其他任何地方使用 $mirror_link
  • @test42132132 你必须在循环之前的某个地方定义$mirror_link
  • 那仍然是模拟输出,它没有从matches[0]中获得输入
猜你喜欢
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多