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

  • Home
  • SEARCH
  • 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 6098491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:52+00:00 2026-05-23T13:09:52+00:00

This seems like an obvious question but there does not appear to be an

  • 0

This seems like an obvious question but there does not appear to be an answer anywhere at the moment.

I am looking to attempt to process a search query to go into a postgresql full-text search with correct formatting using PHP. I am a bit of newbie to regular expressions and I just can’t seem to work out where to start.

I am looking to take a query along the lines ofSomething AND ("Some Phrase" OR Some Other Phrase) and convert it to 'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))

You may be able to point me to a library that does this, I can’t seem to find one although I imagine it is a common problem. Thanks for any help!

  • 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-23T13:09:53+00:00Added an answer on May 23, 2026 at 1:09 pm

    This code will parse the example as given and return the requested output. Be aware that it is somewhat brittle: You may need to validate the format of the expression to ensure that it is similar to the format you posted.

    $input = 'Something AND ("Some Phrase" OR Some Other Phrase)';
    
    $formatted_output = format_input( $input );
    
    // Convert a query to a format suitable for a Postgres full-text search
    function format_input( $input ) {
        $output = '';
        list ( $part1, $part2 ) = explode( 'AND', $input );
    
        // Remove any unecessary characters and add the first part of the line format
        $output = "'" . str_replace( array( "\"", "'" ), '', trim( $part1 ) ) . "' & (";
    
        // Get a list of phrases in the query
        $phrases = explode( 'OR', str_replace( array( '(', ')'), '', $part2 ) );
    
        // Format the phrase
        foreach ( $phrases as &$phrase ) {
            $phrase = encapsulate_phrase( trim ( str_replace( array( "\"", "'"), '', $phrase ) ) );
        }
    
        // Add the formatted phrases to the output
        $output .= '(' . implode( ')|(', $phrases ) . ')';
    
        // Add the closing parenthesis
        $output .= ')';
    
        return $output;
    }
    
    // Split a search phrase into words, and encapsulate the words
    function encapsulate_phrase( $phrase ) {
        $output = '';
        $words = explode( ' ', trim( $phrase ) );
    
        // Remove leading and trailing whitespace, and encapsulate words in single quotes
        foreach ( $words as &$word ) {
            $word = "'" . trim( $word ) . "'";
        }
    
        // Add each word to the output
        $output .= implode (" & ", $words);
    
    
        return $output;
    }
    

    You can test your inputs like this:

    $desired_output = "'Something' & (('Some' & 'Phrase')|('Some' & 'Other' & 'Phrase'))";
    
    if ( !assert ( $formatted_output == $desired_output ) ) {
        echo "Desired: $desired_output\n";
        echo "Actual:  $formatted_output\n";
    }
    else {
        echo "Output: $formatted_output\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a product order table in mysql. It's like this: create table `order`
I've been interested in different chess engines lately. There are many open and closed
I was just looking at the CodeIgniter source code and I came across a
Any advice on how to fix this issue I have, or a better implementation
I have recently (in the last week) embarked on an experiment wherein I attempt
I'm currently working with C++ and .NET 1.1 (Don't ask...), and I'd like to
I am writing an SSRS report that has several parameters including a couple of
I have a window with a list full of Core Data objects, and a
I'm trying to use the new(ish) AS3 global error handling class. I am trying
I've been reading up on PHP lately and Validation of user input is a

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.