1

getlink.html

// all other basic html tags are here which is not need to understand this like(head,body etc)
<a href="website.html">This is the website NO # 1</a>
<a href="http://www.google.com/">This is google site</a>

PHP

<?php
$file = file_get_contents('getlink.html');

$matches = preg_match_all('/<a(?:[^>]*)href=\"([^\"]*)\"(?:[^>]*)>(?:[^<]*)<\/a>/is'
                            ,$file,$match); // work fine
$test1 = preg_match_all('/href=\"((?:https?|ftp)\:\/\/\w+\.[\w-]+\..+)\"/i'
                            ,$file,$test); // work fine

foreach($match[1] as $links) {      

            if ($match[1] == $test[1]){ // write $match[1] not $links
                                        // bcs $links does not work
      echo 'True'.'<br />';
    } else {
     echo 'False'.'<br />';
    }       
                                }

?>

実行すると、1回と2false回ではなく、両方の時間が返されます。falsetrue

2 番目のリンクは と一致する必要があり$test[1]ます。最初のリンクを削除すると、 が返さtrueれます。

助けてください、本当に心配です。

4

3 に答える 3

0

A.あなたは何もしていません$link

B.実行する場合

var_dump($match[1]);
var_dump($test[1]);

出力

array
  0 => string 'website.html' (length=12)
  1 => string 'http://www.google.com/' (length=22)
array
  0 => string 'http://www.google.com/' (length=22)

$test [1]存在しないことがわかりますか

C.使用する必要がありますin_arrayが、複数のTrueが出力されます。

foreach ( $match [1] as $links ) {
    if (in_array ( $links, $test [1] )) {
        echo 'True' . '<br />';
    } else {
        echo 'False' . '<br />';
    }
}

真または偽の使用を取得するにはarray_intersect

$result = array_intersect ( $match [1], $test [1] );
if (count ( $result ) > 0) {
    echo 'True' . '<br />';
} else {
    echo 'False' . '<br />';
}
于 2012-04-29T23:03:41.810 に答える
0
foreach($match[1] as $links) {      

if ($match[1] == $test[1])

$links と呼んでいますが、ループ内でそれを参照していません。

于 2012-04-29T19:28:36.343 に答える
0

$matchesあなたが提供したわずかな情報から推測することしかできませんが、と$test1?の両方にあるリンクを探していると思います。もしそうなら、これはあなたが必要とするものでなければなりません:

foreach($match[1] as $links)
{      

  if (in_array($links, $test[1]))
  {
    echo 'True<br />';
  }

  else
  {
    echo 'False<br />';
  }       

}

これが実際に当てはまる場合は、必要なコードが少ないこれを使用することをお勧めします。

echo count($match[1]) == count(array_diff($match[1], $test1)) ? 'False' : 'True';
于 2012-04-29T19:30:34.127 に答える