Posted By


lancemonotone on 01/27/10

Tagged


Statistics


Viewed 572 times
Favorited by 0 user(s)

substr_in_array()


/ Published in: PHP
Save to your folder(s)

Searches all array elements for a given substring. Returns an array of found key=>value pairs or false if not found.


Copy this code and paste it in your HTML
  1. /**
  2.  *
  3.  * @Search for substring in an array
  4.  *
  5.  * @param string $neele
  6.  *
  7.  * @param mixed $haystack
  8.  *
  9.  * @return mixed Array or false if not found
  10.  *
  11.  */
  12. function substr_in_array($haystack, $needle)
  13. {
  14. $found = array();
  15. /*** cast to array ***/
  16. $needle = (array) $needle;
  17.  
  18. /*** map with preg_quote ***/
  19. $needle = array_map('preg_quote', $needle);
  20.  
  21. /*** loop of the array to get the search pattern ***/
  22. foreach ($needle as $pattern)
  23. {
  24. if (count($found = preg_grep("/$pattern/", $haystack)) > 0) {
  25. return $found;
  26. }
  27. }
  28. /*** if it is not found ***/
  29. return false;
  30. }

URL: http://368design.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.