Page 1 of 1
control a string with an array (bot)
Posted: Sun Jun 28, 2009 3:51 am
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!
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 4:46 am
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.
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 5:05 am
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!
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 5:11 am
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
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 5:12 am
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.
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 6:35 am
by bobb
Yeah now he is always going to consider the first item right?
Is there a way to loop all the 'rule' items?
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 6:46 am
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

Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 7:57 am
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
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 8:05 am
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!
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 8:07 am
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'];
}
}
}
?>
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 8:10 am
by a94060
you may want to run
to see where the value you want to read is being stored. It can also help to check if the value is being stored.
Re: control a string with an array (bot)
Posted: Sun Jun 28, 2009 8:19 am
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...