Home  >  Article  >  Backend Development  >  PHP code for filtering arrays by first character

PHP code for filtering arrays by first character

WBOY
WBOYOriginal
2016-07-25 09:04:43910browse
  1. $array = array(

  2. 'abcd',
  3. 'abcde',
  4. 'bcde',
  5. 'cdef',
  6. 'defg',
  7. 'defgh'
  8. );
  9. $str = '~'.implode('~',$array).'~';
  10. $word = $_GET['word']; //url = xxx.php?word=a
  11. preg_match_all( "/~({$word}(?:[^~]*))/i",$str,$matches);
  12. var_dump($matches[1]);

  13. / /output

  14. //array(2) { [0]=> string(4) "abcd" [1]=> string(5) "abcde" }
  15. //End_php
  16. ?>

Copy code

Instructions: There is a problem when using the above code: there will be problems when using ',' (comma) as the delimiter. Please be careful when using it.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn