2017-01-02 107 views
0

我正在使用一个简单的html dom来解析html文件。PHP中的数组过滤器

我有一个动态数组称为links2,它可以是空的或也许有内部或多个视情况而定

<?php 
include('simple_html_dom.php'); 

$url = 'http://www.example.com/'; 

$html = file_get_html($url); 
$doc = new DOMDocument(); 
@$doc->loadHTML($html); 
////////////////////////////////////////////////////////////////////////////// 
foreach ($doc->getElementsByTagName('p') as $link) 
{ 

    $intro2 = $link->nodeValue; 

    $links2[] = array(
     'value' => $link->textContent, 

    ); 

    $su=count($links2); 
} 

$word = 'document.write('; 

假设两个元件包含在“阵列links2” $字4层的元件,当我试图通过去除元件来过滤这个“数组links2”包含比赛

unset($links2[array_search($word, $links2)]); 
print_r($links2); 

的滤波器去除只有一个元素和array_diff不解决问题。任何建议?

+0

它是否包含搜索词不要添加'textContent'到阵列? –

+0

感谢您的快速回放,以及我在这段代码中需要的东西解决! – filip

回答

0

通过添加例外

解决
foreach ($doc->getElementsByTagName('p') as $link) 
{ 
$dont = $link->textContent; 
if (strpos($dont, 'document') === false) { 




    $links2[] = array(
     'value' => $link->textContent, 

             ); 
    } 
    $su=count($links2); 
    echo $su;