-2

I have created a search for on my website and dependent what term is written in the string, it needs to run a different php code.

For example people could type in #rtp drum and bass music it will then do the php code for that.

Another is if some types in “radio that plays drum and bass music” I will then need it to run a different code.

Finally, some one could type #rtp robbie williams songs it will also need it to run different code. My code below does not work though. Any ideas?

if(preg_match('[#rtp|music]', $a)){
    preg_match('/#rtp (.*) music/', $a, $match);
    $tags = $match[1];
}

if(preg_match('[radio|that|plays|music]', $a)){
    preg_match('/plays (.*) music/', $a, $match);
    $tags = $match[1];
}

if(preg_match('[#rtp|songs]', $a)){
    preg_match('/#rtp (.*) songs/', $a, $match);
    $tags = $match[1];
}

$tags = explode(' ', $tags);
$tags = implode("%' OR `tags` LIKE '%", $tags);
$result1 = mysql_query("SELECT * FROM `Stations` WHERE `tags` LIKE '%$tags%'" );
$num_rows1 = mysql_num_rows($result1);

echo $num_rows1;
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Jamie Warren
  • 123
  • 1
  • 9

2 Answers2

0

Looking at your code, perhaps using parenthesis would work as expected. When you use [#rtp|music] all that is getting matched is the #. Check it out here. Parenthesis captures the whole string your regex implies should be captured. Also, use else if for the additional checks. Here is my rewritten code.

if (preg_match('(#rtp|music)', $a)){
    preg_match('/#rtp (.*) music/', $a, $match);
    $tags = $match[1];
}

else if (preg_match('(radio|that|plays|music)', $a)){
    preg_match('/plays (.*) music/', $a, $match);
    $tags = $match[1];
}

else if (preg_match('(#rtp|songs)', $a)){
    preg_match('/#rtp (.*) songs/', $a, $match);
    $tags = $match[1];
}

$tags = explode(' ', $tags);
$tags = implode("%' OR `tags` LIKE '%", $tags);
$result1 = mysql_query("SELECT * FROM `Stations` WHERE `tags` LIKE '%$tags%'" );
$num_rows1 = mysql_num_rows($result1);

echo $num_rows1;

That said—and I bet I will not be the only one saying this—but you should replace all your calls using mysql_* extensions to mysqli_* extensions since not only is mysql_* depreciated as of PHP 5.3, but is completely gone in PHP 5.5. More details here.

Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

You must replace the two last if with elseif, otherwise $tags will be overwritten.

if(preg_match('[#rtp|music]', $a)){
    preg_match('/#rtp (.*) music/', $a, $match);
    $tags = $match[1];
}
elseif(preg_match('[radio|that|plays|music]', $a)){
    preg_match('/plays (.*) music/', $a, $match);
    $tags = $match[1];
}
elseif(preg_match('[#rtp|songs]', $a)){
    preg_match('/#rtp (.*) songs/', $a, $match);
    $tags = $match[1];
}

if (!empty($tags)) {
    ...

As an aside comment, using square brackets as pattern delimiters is not the best choice of the universe since it is confusing with character classes. I suggest to replace them with a slash:

if(preg_match('/#rtp|music/', $a))
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125