繁体   English   中英

in_array检查后返回数组结果?

[英]returning array results after in_array check?

我正在尝试读取包含HTML列表项(以++++为分隔符)的文件。 基本上,我试图在数组的每个项目中搜索“ nmb”的文本(存储在类中),然后在数组中显示所有具有“ nmb”的项目。 但是,我当前的代码不起作用。 它显示...无。 并没有错误。 我在这里想念什么?

<?php
$term = "nmb";
$f_contents = file_get_contents("../includes/inc-condo-list.php"); //get the entire file
$array = explode("++++",$f_contents); //explode (create an array) seperated by new line elements
$datotal = count($array);  //gives me a TOTAL count.. maybe for later use


foreach ($array as $str ){    //go through each item in array
    if(!in_array($term, $array)) {
    echo $str;
    }
}

unset ($array);

?>

这是读取文件时引入的列表项的示例。

  ++++
    <li class="condo mb"> <a class="condos" href="blah.html"> <img src="someimg.jpg" alt="Narf" /></a> <br />
     <a class="condos" href="somewhere.html">Some Link</a><br />
        4 &amp; 6 Bedrooms<br />

</li>
    ++++
  <li class="condo nmb"> <a class="condos" href="blah.html"> <img src="someimg.jpg" alt="Narf" /></a> <br />
     <a class="condos" href="somewhere.html">Some Link</a><br />
        4 &amp; 6 Bedrooms<br />

</li>
    ++++

任何帮助将不胜感激! 谢谢。

in_array寻找精确的值匹配。 使用类似这样的东西。

foreach ($array as $str ){    //go through each item in array
    if(strpos($str, $term) === false) { // if it's not present
        echo $str;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM