Posted By

engel on 12/30/07


Tagged

array strstr stristr


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

heinz1959


strstr() and stristr() with Arrays


 / Published in: PHP
 

Searches an array $haystack for $needle. Returns the value of the element which contains the first result. Use strstrarray() for case-sensitive searches and stristrarray() for case-insensitive searches.

  1. <?php
  2.  
  3. function strstr_array( $haystack, $needle ) {
  4. if ( !is_array( $haystack ) ) {
  5. return false;
  6. }
  7. foreach ( $haystack as $element ) {
  8. if ( strstr( $element, $needle ) ) {
  9. return $element;
  10. }
  11. }
  12. }
  13.  
  14. function stristr_array( $haystack, $needle ) {
  15. if ( !is_array( $haystack ) ) {
  16. return false;
  17. }
  18. foreach ( $haystack as $element ) {
  19. if ( stristr( $element, $needle ) ) {
  20. return $element;
  21. }
  22. }
  23. }
  24.  
  25. ?>

Report this snippet  

You need to login to post a comment.