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 6738571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:23:04+00:00 2026-05-26T11:23:04+00:00

i need to parse a search string for keywords and phrases in php, for

  • 0

i need to parse a search string for keywords and phrases in php, for example

string 1: value of "measured response" detect goal "method valuation" study

will yield: value,of,measured reponse,detect,goal,method valuation,study

i also need it to work if the string has:

  1. no phrases enclosed in quotes,
  2. any number of phrases encloses in quotes with any number of keywords outside the quotes,
  3. only phrases in quotes,
  4. only space-separated keywords.

i’m leaning towards using preg_match with the pattern '/(\".*\")/' to get the phrases into an array, then remove the phrases from the string, then finally work the keywords into the array. i just can’t pull everything together!

i’m also thinking of replacing spaces outside quotes with commas. then explode them to an array. if that’s a better option, how do i do that with preg_replace?

is there a better way to go about this? help! thanks much, everyone

  • 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-26T11:23:05+00:00Added an answer on May 26, 2026 at 11:23 am
    preg_match_all('/(?<!")\b\w+\b|(?<=")\b[^"]+/', $subject, $result, PREG_PATTERN_ORDER);
    for ($i = 0; $i < count($result[0]); $i++) {
        # Matched text = $result[0][$i];
    }
    

    This should yield the results you are looking for.

    Explanation :

    # (?<!")\b\w+\b|(?<=")\b[^"]+
    # 
    # Match either the regular expression below (attempting the next alternative only if this one fails) «(?<!")\b\w+\b»
    #    Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!")»
    #       Match the character “"” literally «"»
    #    Assert position at a word boundary «\b»
    #    Match a single character that is a “word character” (letters, digits, etc.) «\w+»
    #       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    #    Assert position at a word boundary «\b»
    # Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «(?<=")\b[^"]+»
    #    Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=")»
    #       Match the character “"” literally «"»
    #    Assert position at a word boundary «\b»
    #    Match any character that is NOT a “"” «[^"]+»
    #       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a XML into a string based on user's search input
I need to parse the following node: <media:keywords>keyword1,keyword2<![CDATA[keyword3]]></media:keywords> into a valid string, preferably keyword1,keyword2,keyword3
I need ActionScriot code that will parse a given folder and search it for
I need to parse a string like February 12, 1981 as a Date. I
i have to parse need string. Here is command I execute in Linux console:
I need to write a method that takes a String and parses it for
I have a string I need to parse. It meets the following requirements: It
I need to parse through an XML document in the database and search for
I need parse through a file and do some processing into it. The file
I have a rather big number of source files that I need parse and

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.