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

PHP code for filtering arrays by first character

WBOY
Release: 2016-07-25 09:04:43
Original
937 people have browsed it
  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.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template