strstr() and stristr() with Arrays


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

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.