0

I have a little tag cloud that shows the last 20 searches and I am trying to make it so it will never show any keywords that start with the number 0 or 1. I have a problem with google searching for random crap starting with 0 and 1. Google is inputing crap like
0 1 1 0 0‰9 1�è7 0‡0�§7
into my form and it makes my tag cloud look like crap so I would like it to not show.

Here is basically what I am using to display tags right now.

$num = '1';
$result=mysql_query("select keyword, time from keywords order by time DESC LIMIT 20");
while (($row=mysql_fetch_row($result)) && ($num <= '20')) {
$word = $row[0];
$time = $row[1];
echo "".$word."";
$num++;
}

$word is what I need to filter and display. I have tried to figure out a way with preg_replace but can only get it to remove the first number.

Does anyone know how I can remove all words starting with 0 or 1 in $word? Thanks.

2 Answers 2

2

You can skip them from the database select

select keyword, time from keywords WHERE keyword REGEXP '^[:alpha:]' ORDER by time DESC LIMIT 20

This will get from the database only keywords that start with a alpha char

If you don't want to use the select, you can do it in php with something like:

$result=mysql_query("select keyword, time from keywords order by time DESC LIMIT 20");
while ($row=mysql_fetch_row($result)) {
    if (preg_match('^\w',$row[0])) {
        $word = $row[0];
        $time = $row[1];
        echo "".$word."";
    }
}

This will echo only the words that start with a alpha char.

1
  • the sql query seems to work great. I had to remove order before WHERE in your sql. I think that is a typo of yours. Other than that, works excellent. Thanks.
    – chris
    Dec 30, 2011 at 8:51
0

Not, totally sure what ur asking. But you could do,

if(substr($word, 0, 1) === 0 || substr($word, 0, 1) === 1) {
    $word = '';
    }

That way if the first character of $word is 0 or 1 it will return NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.