Home  >  Article  >  Backend Development  >  PHP sample code for filtering the first character of an array

PHP sample code for filtering the first character of an array

怪我咯
怪我咯Original
2017-07-07 09:28:001136browse

phpIn the array, the array needs to be filtered by the first character. This is achieved through the following code. Friends in need can refer to the

code as follows:

<?php 
$array = array( 
&#39;abcd&#39;, 
&#39;abcde&#39;, 
&#39;bcde&#39;, 
&#39;cdef&#39;, 
&#39;defg&#39;, 
&#39;defgh&#39; 
); 
$str = &#39;~&#39;.implode(&#39;~&#39;,$array).&#39;~&#39;; 
$word = $_GET[&#39;word&#39;]; //url = xxx.php?word=a 
preg_match_all("/~({$word}(?:[^~]*))/i",$str,$matches); 
var_dump($matches[1]); 

//输出 
//array(2) { [0]=> string(4) "abcd" [1]=> string(5) "abcde" } 
//End_php

The above is the detailed content of PHP sample code for filtering the first character of an array. For more information, please follow other related articles on the PHP Chinese website!

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