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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:15:53+00:00 2026-05-19T23:15:53+00:00

I am stuck again. I have a search string and assigned it to a

  • 0

I am stuck again.

I have a search string and assigned it to a variable:

my $search =  'foo bar blue widget'
$search =~ s/[\n\r]+|^\s+|\s+$//g; #remove leading and trailing spaces n stuff

I was using

SELECT FROM `sometable` WHERE `coltosearch` LIKE '%$search%'

But, would match only “foo bar blue widget” OR “foo”.

I want to match “foo bars” or “foobar” too.

So, I think I would have to do:

 my $search =  'foo bar blue widget';
    $search =~ s/[\n\r]+|^\s+|\s+$//g; #remove leading and trailing spaces n stuff
 my $search1 =  $search;
    $search1 =~ s/^\s+|\s+$|\s*//g; #remove all spaces

   SELECT FROM `sometable` WHERE `coltosearch` LIKE '%$search%' OR LIKE '%$search1%'

Well, that may be fine for “foo bar blue widget” and “foobarbluewidget”. (if it worked)

So, I am guessing I will have to loop through $search, and assign variables to each word.

But, I never know how many words I will have in $search.

So, I am stuck. Could I do something like:

my $search =  'foo bar blue widget'; #in from search form. Actually a $var
$search1 =~ s/^\s+|\s+$|\s*//g; #remove all spaces
$search1 = "%$search1%"; # Makes %foobarbluewidget%

## Now make individual words somehow ##
my @searcharry;
my $cnt='0';
my $searchword;
my @searchsplit = $search;
while $searchword (@searchsplit){
push (@searcharry,$searchword[$cnt]);
$cnt++;
}

But how do I get @searcharry into:

$search =~ s/\s/%/gi;
$search =~ s/%%/%/gi; #I think makes %foo%bar%blue%widget%

So I can:

 SELECT FROM `sometable` WHERE `coltosearch` LIKE '$search' OR LIKE '$search1'

I am probably way off base. This is really difficult for me.
I just want to match any words in the search string or match if all words are run together.

Sorry for being such a noob. My crude attempt, I believe is an insult to Perl and real coders.

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-19T23:15:53+00:00Added an answer on May 19, 2026 at 11:15 pm

    If you have $string = "foo bar blue widget", then you’d do something like:

    my @values = split '\s+', $string;
    my $where = "WHERE $col LIKE ?";
    $where .= " OR LIKE ?" x $#values;
    

    This will create a where clause of WHERE column LIKE ? OR LIKE ? OR LIKE ? OR LIKE ? for your "foo bar blue widget" string.

    Then you use DBI placeholders and bind values so you don’t have to worry about SQL injection attacks on your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which contains some search fields to query a database, and
Being stuck with a legacy database schema that no longer reflects your data model
Really stuck on trying to write code to unzip a file or directory on
Disclaimer: I'm stuck on TFS and I hate it. My source control structure looks
I'm a bit stuck on this. Basically I want to do something like the
I'm stuck on a PHP 4 server, and I would like to start moving
I'm stuck on a fix to a legacy Visual C++ 6 app. In the
I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies
I'm kinda stuck with this one so I hoped someone could help me. I
I'm stuck in .NET 2.0 Windows Forms. It doesn't look like the ability to

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.