1

I have a search text field, and an array I want to output below.

This is my array:

array=["abc","abcde","ab","abcdef"];

When I enter "ab" in the text field, then the list should appear. "ab" should come first.

ab,
abc,
abcde,
abcdef,

If I type "abc" then list should show:

abc,
abcde,
abcdef,
Simulant
  • 19,190
  • 8
  • 63
  • 98
Sam
  • 35
  • 2

2 Answers2

0

This should solve your little issue

<?php
$input = preg_quote('cde', '~'); // don't forget to quote input string!
$array=["abc","abcde","ab","abcdef"];
$result = preg_grep('~' . $input . '~', $array);
foreach ($result as $val) {
    echo "$val\n";
}
?>

Check this link for more

James Bondze
  • 102
  • 1
  • 8
  • Thank you for your replay:- If I search for "cde" I have to get Below result :- abcde abcdef But it gives me the same – Sam Mar 07 '19 at 09:01
0
<?php
$array = ["abc","abcdeab","ab","abcdef"];
$arr = preg_grep('/cde/', $array);
sort($arr);
var_dump($arr);
?>

check this out

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19