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

4 Answers 4

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.

4
  • $var is an array - just saying Jul 24, 2015 at 5:08
  • @Feanne - you understand that this will match *alpha, +alpha, %alpha as well as the #alpha though - just thought I would mention that. Oddly enough only my answer will match #alpha exactly and no other starting word. Jul 24, 2015 at 7:08
  • @ArtisiticPhoenix his search word is #alpha, then how it would match *alpha Jul 24, 2015 at 7:18
  • sorry I was confusing \B don't see that used much, + its 330am here, my bad Jul 24, 2015 at 7:30
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); });
2
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

2
  • again the lack of the ^ anchor means this will also match beta #alpha as well as b#alpha etc. Jul 24, 2015 at 7:12
  • @ArtisiticPhoenix Question was: ...Search value contains hashtag
    – Jonny 5
    Jul 24, 2015 at 7:20
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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.