Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3224044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:10:38+00:00 2026-05-17T16:10:38+00:00

I am using preg_match_all to search for HashTag values in a Twitter Search response.

  • 0

I am using preg_match_all to search for HashTag values in a Twitter Search response.

It works as I expected except for when the search results don’t have any hash values in them. For some reason my $tags array still has values and I’m not sure why.

Is it because my RegEx is not correct, or is it a problem with preg_match_all?

Thanks

$tweet = "Microsoft Pivot got Runner-Up for Network Tech from The Wall Street Journal in 2010 Technology Innovation Awards  http://bit.ly/9pCbTh";

private function getHashTags($tweet){
    $tags = array();
    preg_match_all("/(#\w+)/", $tweet, $tags);

    return $tags;

}

results in:

Array ( [0] => Array ( ) [1] => Array ( ) )

Expected results:

Array();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:10:38+00:00Added an answer on May 17, 2026 at 4:10 pm

    In default mode, preg_match_all returns an array of matches and submatches:

    PREG_PATTERN_ORDER
    Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.

    So in this case the first array is the array of matches of the whole pattern and the second array is the array of matches of the first subpattern. And since there was no match found, both arrays are empty.

    If you want the other order, having each match in an array with its submatches, use PREG_SET_ORDER in the flags parameter:

    preg_match_all("/(#\w+)/", $tweet, $tags, PREG_SET_ORDER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want
I'm using PHP's preg_match_all() to search a string imported using file_get_contents(). The regex returns
Consider that i have the string, $string = 'tag2 display=users limit=5'; Using the preg_match_all
Im grabbing details using preg_match_all but i have a wired html tag placement to
I need to print all matches using preg_match_all. $search = preg_match_all($pattern, $string, $matches); foreach
I'm using preg_match_all to search through a file that I'm reading in. The file
I have a problem with regex, using preg_match_all(), to match something of a variable
i have a simple php example of using preg_match_all $str = Line 1: This
i have a string and im using preg_match_all to find some tags in that
i'm using preg_match_all('/<?(.*)?>/', $bigString, $matches, PREG_OFFSET_CAPTURE); to find the contents of everything between <?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.