7

Example:

$array = array('alpha beta','beta gamma','delta phi', '#alpha phi', 'beta phi');
$searchword = 'alpha';
$results = array_filter($array, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
print_r($results);

Array ( [0] => alpha beta [3] => #alpha phi )

I would like to find only the elements containing #alpha, not alpha.

The result I want is Array ( [3] => #alpha phi ).

However, this doesn't work:

$array = array('alpha beta','beta gamma','delta phi', '#alpha phi', 'beta phi');
$searchword = '#alpha';
$results = array_filter($array, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
print_r($results);

Array ( )

I also tried $searchword = preg_quote('#alpha'); but that doesn't work either. I'm not familiar enough with regex to figure this out-- there must be some regex rule I'm missing out on?

Note: I am not looking to find all the hashtag keywords in the array. I want to search for a specific hashtag keyword.

Credit: I used one of the answers from here: Search for PHP array element containing string

Community
  • 1
  • 1
Feanne
  • 473
  • 1
  • 5
  • 10

4 Answers4

2

Remove the starting \b

preg_match("/$searchword\b/i", $var);

or

Use \B

preg_match("/\B$searchword\b/i", $var);

Why?

It's all about word boundaries \b matches between word char and a non-word char or vice-versa. Since the first character is # which is a non-word character and before that character there exists the start of the line boundary. \B is the suitable one for this, which matches between two non-word chars or two word chars. Here start of the line , # so there must exists \B in-between not \b.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
2

Drop the first \b word boundary and use $searchword = '#alpha';
To search an array and get entries matching the pattern use preg_grep function:

print_r(preg_grep("/".preg_quote($searchword, "/")."\b/i", $array));

Array ( [3] => #alpha phi )

Test at eval.in

Jonny 5
  • 12,171
  • 2
  • 25
  • 42
2

Or you can also use strpos instead of preg_match as

$searchword = '#alpha';
$results = array_filter($array, function($var) use ($searchword) { return (strpos($var,$searchword)>-1); });
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

Change your code

 $searchword = 'alpha';

To

$searchword = '^#alpha';

Starts with ^ # hashtag,

 preg_match('/^#alpha/', '#alphasomething');

Will match #alpha

https://regex101.com/r/rU1eA9/1

Doesn't match alpha

https://regex101.com/r/rU1eA9/2

Seeing as this is open to interpenetration

I would like to find only the elements containing #alpha

Some answers may match a variety of things that "contain" #alpha such as b#alphagama or in some cases gamma #alpha etc.

I though I would mention the the leading ^ carrot in my RegX means begins with. So in order for my answer to match the string must begin with a # hashtag followed by alpha literally.

So my answer may not be the best if you are just wanting the "contains" #alpha such as gama#alpha. But if you want begins with #alpha I would recommend using it.

I feel it's important to make sure I high lighted these edge cases, and exactly what the leading ^ means in mine.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38