Jump to content

[SOLVED] wildcards with a string


dreamwest

Recommended Posts

I cant seem to get this to work...

Im trying to use wildcards within a search string:

 

SELECT *  FROM `tags` WHERE tag like '%looking%anything%'

 

I want to match anything either side of each word, so logically it would look like this:

 

SELECT *  FROM `tags` WHERE tag like '%looking%'

 

And

 

SELECT *  FROM `tags` WHERE tag like '%anything%'

Link to comment
Share on other sites

SELECT *  FROM `tags` WHERE tag IN('%looking%', '%anything%');

 

Thanks. But is there a way to search each word individually without using explode to separate the string

 

The only solution i can think of is to strreplace the spaces and set it as a variable

 

$search = "looking anything"
$search = str_replace( " ", "%', '%", $search );
SELECT *  FROM `tags` WHERE tag IN('%$search%');

 

 

Link to comment
Share on other sites

But is there a way to search each word individually without using explode to separate the string

 

What's wrong with explode?

 

It might have more or less than 2 words in the string

 

So if the string is 1 word, this code will search for tags with a zero value because $pieces[1] is nothing :

$sql4 = "SELECT * from tags where (tag like '%".$pieces[0]."%' OR tag like '%".$pieces[1]."%')";

Link to comment
Share on other sites

Hi

 

You could use explode and then implode (although you could also do it using a regular expression).

 

$pieces = explode(" ", $search);
$pieceStr = implode("%' OR tag like '%", $search);
$sql4 = "SELECT * from tags where (tag like '%$pieceStr%')";

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

You could use explode and then implode (although you could also do it using a regular expression).

 

$pieces = explode(" ", $search);
$pieceStr = implode("%' OR tag like '%", $search);
$sql4 = "SELECT * from tags where (tag like '%$pieceStr%')";

 

All the best

 

Keith

 

Excellent. Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.