简体   繁体   中英

returning array results after in_array check?

I am trying to read in a file that has HTML list items with ++++ as delimeters. Basically, I am trying to search through each item in the array for the text of "nmb" (which is stored in the class) and then display all of the items IN the array that have "nmb". However, my current code does not work. It displays... NOTHING. And gives no errors. What am I missing here?

<?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);

?>

Here is an example of the list items being brought in from reading the file.

  ++++
    <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>
    ++++

Any help would be MORE than appreciated! Thank you.

in_array looks for an exact value match. Use something like this instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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