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
https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/
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%');

 

 

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]."%')";

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.