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 />';
}
}
?>
当我运行它时,它两次都返回false
,而不是一次false
和第二次true
。
第二个链接应与$test[1]
匹配。如果我删除第一个链接,它会返回true
,
请帮助我,我真的很担心。
foreach($match[1] as $links) {
if ($match[1] == $test[1])
你称它为$links但不是指循环中的它。
我只能用你提供的很少的信息来猜测,但我假设你正在寻找$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';
A.你没有做任何事情会$link
二.如果您运行
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]
不存在吗
三.你应该做的是使用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 />';
}