control a string with an array (bot)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobb
Forum Newbie
Posts: 18
Joined: Sun Jun 28, 2009 3:41 am

control a string with an array (bot)

Post by bobb »

Hello,

I got this array (just a small piece of the total):

Code: Select all

<?
$robot = array (
"yell" => array(
    "icon" => "yell",
    "title" => "Yell",
    "rule" => array(
      "YellCrawl[ /]V?([0-9.]{1,10})" => "\\1",
      "Yellbot[ /]Nutch-([0-9.]{1,10})" => "\\1",
    )
  ),
  "yodao" => array(
    "icon" => "robot",
    "title" => "Yodao",
    "rule" => array(
      "YodaoBot(-Image|)?[ /]([0-9.]{1,10})" => "\\2"
    ),
    "uri" => "http://www.yodao.com/help/webmaster/spider/"
  ),
  "yoogli" => array(
    "icon" => "yoogli",
    "title" => "Yoogli",
    "rule" => array(
      "yoogliFetchAgent[ /]([0-9.]{1,10})" => "\\1"
    ),
    "uri" => "http://www.yoogli.com"
  ),
  "yotta" => array(
    "icon" => "robot",
    "title" => "Yotta",
    "rule" => array(
      "Yotta(Shopping|Cars)_Bot[ /]([0-9.]{1,10})" => "\\2",
      "OmniExplorer_Bot[ /]([0-9.]{1,10})" => "\\1"
    ),
    "uri" => "http://www.yottacars.com"
  )
);
?>
Now I want to check if a webbot visited my website:

Code: Select all

<?
 
foreach($robot as $value) {
  if(preg_match('_'.preg_quote($value['rule'][1], '_').'_', $user_agent)) {
    
    $webbot = $value['title'];
  }
}
?>
 
But then I get this error:

Code: Select all

Notice: Undefined offset: 1 in C:\www\value.class.php on line 5398
How can I fix this?
Thanks!
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Hello,

This will be happening due to the nested array "rule" which doesnt have a key of 1. The error is basically saying, it cant find an item in the array mentioned with a key of 1 so it might be worthwhile looking at what keys are available and adjust your loop to pull the correct key.

Thats my first impression, if it doesnt work let me know, I havnt had a chance to test your code.

Thanks.
bobb
Forum Newbie
Posts: 18
Joined: Sun Jun 28, 2009 3:41 am

Re: control a string with an array (bot)

Post by bobb »

juma929 wrote:Hello,

This will be happening due to the nested array "rule" which doesnt have a key of 1. The error is basically saying, it cant find an item in the array mentioned with a key of 1 so it might be worthwhile looking at what keys are available and adjust your loop to pull the correct key.

Thats my first impression, if it doesnt work let me know, I havnt had a chance to test your code.

Thanks.

How do you mean with looking at what keys are available? How do I do this?

Thank you!
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Hello,

Try changing:

if(preg_match('_'.preg_quote($value['rule'][1], '_').'_', $user_agent)) {

to,

if(preg_match('_'.preg_quote($value['rule'][0], '_').'_', $user_agent)) {

Thanks
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Some of your rule arrays dont have more than one item in them, so when you are trying to access key 1, it cant find an item there. I would research PHP arrays a little to find out how the keys work because I could say they all start with 0 but that would give you a false impression which you will discover if you do a little reading on the topic.

Thanks.
bobb
Forum Newbie
Posts: 18
Joined: Sun Jun 28, 2009 3:41 am

Re: control a string with an array (bot)

Post by bobb »

Yeah now he is always going to consider the first item right?

Is there a way to loop all the 'rule' items?
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Hello,

In this case there are many ways to accomplish the same task, a quick approach would be:

Code: Select all

 
<?
foreach($robot as $value) {
  for($i=0; $i<count($value['rule']); $i++)
  {
     if(preg_match('_'.preg_quote($value['rule'][$i], '_').'_', $user_agent)) {
       $webbot = $value['title'];
     }
  }
}
?>
 
There are obviously problems with this is that the last found rule will be stored into the $webbot variable. If you just wanted to select the last element which this current code would do, you could simplify it by doing something like this (also many ways):

Code: Select all

 
<?
foreach($robot as $value) {
   $test_value = end($value['rule']);
 
   if(preg_match('_'.preg_quote($test_value, '_').'_', $user_agent)) {
     $webbot = $value['title'];
   }
}
?>
 
As I say there are many ways to do the same job, but it depends if you need to store all found web bots or just handle the last found web bot.

Thanks :)
bobb
Forum Newbie
Posts: 18
Joined: Sun Jun 28, 2009 3:41 am

Re: control a string with an array (bot)

Post by bobb »

I tried it.. but i get this then:

Code: Select all

 
Notice: Undefined offset: 0 in value.class.php on line 5400
 
Notice: Undefined offset: 1 in value.class.php on line 5400
 
Notice: Undefined offset: 0 in value.class.php on line 5400
 
Notice: Undefined offset: 0 in value.class.php on line 5400
 
Notice: Undefined offset: 0 in value.class.php on line 5400
 
Notice: Undefined offset: 0 in value.class.php on line 5400
 
Notice: Undefined offset: 1 in value.class.php on line 5400
 
And so on....
 
:s
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Hello,

SORRY, totally misread your code, thats what rushing around does for you. Your array doesnt have numeric keys, therefore they cant be accessed by using a number, you have to access the keys using the corresponding string.

Thanks, sorry again!
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: control a string with an array (bot)

Post by juma929 »

Hello,

The following will run through the code and supply the correct key to the preg_match line regardless of the numeric key issues etc...

Code: Select all

 
 <?
 foreach($robot as $value) {
   foreach($value['rule'] as $keyb => $valueb)
   {
      if(preg_match('_'.preg_quote($value['rule'][$keyb], '_').'_', $user_agent)) {
        $webbot = $value['title'];
      }
   }
 }
 ?>
 
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: control a string with an array (bot)

Post by a94060 »

you may want to run

Code: Select all

print_r($value)
to see where the value you want to read is being stored. It can also help to check if the value is being stored.
bobb
Forum Newbie
Posts: 18
Joined: Sun Jun 28, 2009 3:41 am

Re: control a string with an array (bot)

Post by bobb »

I feel we are getting there, the errors are gone.. but

I still have one problem, he always says he matches with the last array,to say it in a simplified way:

array = A, B, C

string = B
match array string? ->

He always outputs C...
Post Reply