[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP : Weird results with explode and array_intersect

Posted on 2009-04-29
21
Medium Priority
?
386 Views
Last Modified: 2012-05-06
Hi,

I'm having this piece of code to parse a bit of text into an array and then compare it with
keys i define in another array. However if i compare it and i do not have another array value
after that value i'm checking on, the check will fail and i will get no result,

Example :

Array ( [5] => TEST ) // The intersect result

Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) // It's found in this array.

Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST ) // It's however, not found in this array.

I don't get it ! can anyone help me ?

The work around to add another value so the check will work is unfortunatly not possible in my case =).

The code i'm using is just a test case - it is not a final code- so save your markup tips.



$string = "
4/28 16:14:50.187  Tester whispers: 200 TEST not last
4/28 16:14:50.328  To Tester: 200 TEST
4/28 16:30:55.875  Joined Channel: |Hchannel:1699248|h[(null)]|h";
 
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
 
foreach ($string as $key => $val) {
 
  $val = str_replace("[","",$val); // removes the brackets
 
  $val = str_replace("]","",$val); // removes the brackets
 
  $val= str_replace("  "," ",$val); // removes double spaces
 
  $array[] = explode(" ",$val); // Saves to array, seperated word by word
 
  print_r(array_intersect($array[$i],$function_array));
 
  print_r($array[$i]);
 
  echo "<br>";
 
  $i++;
 
}

Open in new window

0
Comment
Question by:Nielsdv
  • 13
  • 8
21 Comments
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24258844
hi,

i am very puzzled about this line

print_r(array_intersect($array[$i],$function_array));


array_intersect needs two or more arrays two work, $array[$i] is a string so you need to probably remove the $i from there. And the print_r will display the intersection of values from the two arrays (not keys).

another problem is with print_r($array[$i]); again do you want to see the $i th word from the current line (the $i being the current line count)(doesnt really make sense)

IF you want to save the lines in $array then the line needs to be

$array[$i][] = explode(" ",$val); // Saves to array, seperated word by word

and the future calls to this array needs to be $array[$i][] (for the intersect and the following print_r functions)

hope I got your intentions right and that this helps
0
 

Author Comment

by:Nielsdv
ID: 24258869
$array is a two dimensional array =)
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24258875
and the corected code to my guess
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
 
$array = array(); //always init vars :)
 
foreach ($string as $key => $val) {
 
  //its good practice not to modify $key or $val
 
  $vals = str_replace("[","",$val); // removes the brackets
 
  $vals = str_replace("]","",$vals); // removes the brackets
 
  $vals= str_replace("  "," ",$vals); // removes double spaces
 
  $array[$i][] = explode(" ",$vals); // Saves to array, seperated word by word
 
  print_r(array_intersect($array[$i][],$function_array));
 
  print_r($array[$i][]);
 
  echo "<br>";
 
  $i++;
 
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Nielsdv
ID: 24258887
Okay, i see you can't eddit comments on EE, anyway.

It's an two dimensional array say ;

Each loop it makes a new entry in the array, and i use i to define wich sub-array i want to calculate.
Giving that the code isn't really optimal, the array_intersect works fine, on every array !
Aslong it isn't the last word in the sub-array.
0
 

Author Comment

by:Nielsdv
ID: 24258903
The code your posted doesn't work because intersect can't read an undefined []

"Fatal error: Cannot use [] for reading in /home/nielsdv/domains/nielsdv.nl/public_html/project/parser.php on line 202"

Anyway- the point is that it DOES actually work and it's already a two dimensional array.

The only problem is - it doesnt work if let's say out of 6 values , the 6th value is the one you're looking for.

It does work if let's say out of 6 values the 5th one is the one you're looking for.
0
 

Author Comment

by:Nielsdv
ID: 24258920
$array[] -> new array entry -> explode is an array result.

  so array in array

   $array[0] => array[0]=> array[1] =>
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259051
yup, your right there, i missed the brackets (maybe i shouldnt post stuff half asleep :)) ) let me take another look at it
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259128
hi,

ive just run your code and it seems to work correctly, check the attached code (ive only added a few echos).
$string = "
4/28 16:14:50.187  Tester whispers: 200 TEST not last
4/28 16:14:50.328  To Tester: 200 TEST
4/28 16:30:55.875  Joined Channel: |Hchannel:1699248|h[(null)]|h";
 
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
 
 
foreach ($string as $key => $val) {
 echo "curent line: " . $val . "<br>";
  $val = str_replace("[","",$val); // removes the brackets
 
  $val = str_replace("]","",$val); // removes the brackets
 
  $val= str_replace("  "," ",$val); // removes double spaces
 
  $array[] = explode(" ",$val); // Saves to array, seperated word by word
 echo "Intersect: <br>";
  print_r(array_intersect($array[$i],$function_array));
 echo "<br>the array: <br>";
  print_r($array[$i]);
 
  echo "<hr>";
 
  $i++;
 
}

Open in new window

0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259138
maybe you miscounted the output lines? (be aware that theres an empty line at the start of the string, which is gonna give two empty arrays at start)
0
 

Author Comment

by:Nielsdv
ID: 24259312
The output of this file is giving a good example;


curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )

So as you can see the intersect is empty on the second one, while there IS test in the text.

That shouldn't be happening ?
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259342
heres the (stripped)output that im getting when im running the script:


curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last )

curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )


as you can see im getting the correct output, can you tell me more about the environment in which you test the script?
0
 

Author Comment

by:Nielsdv
ID: 24259356
Also, when i add asmuch as a spacebar in the source (so theres another value in the array) it DOES pick it up :S
0
 

Author Comment

by:Nielsdv
ID: 24259363
http://www.nielsdv.nl/project/parser.php <- here is my script ran from, its on PHP5 Environment
0
 

Author Comment

by:Nielsdv
ID: 24259401
okay i just called up my phpinfo , and i was quite suprised with the results :

http://www.nielsdv.nl/project/phpinfo.php
0
 

Author Comment

by:Nielsdv
ID: 24259464
Okay i ran php info i got chinese :

!OTP tlPBI -/3/DDXTL10Tastoa/E""T/hm1tastoa.t"
hm>ha>bd bcgon-oo:#fff oo:#000}bd,t,t,h,h fn-aiy assrf}pe{agn p;fn-aiy oopc;
:ik{oo:# 009 etdcrto:nn;bcgon-oo:#fff}ahvr{etdcrto:udrie}tbe{odrclas:clas;
cne tx-lg:cne;
cne al  agnlf:at;mri-ih:at;tx-lg:lf;
cne h{tx-lg:cne iprat
d h {bre:1xsld#000 otsz:7% etclain aeie}h fn-ie 5%}h fn-ie 2%}. tx-lg:lf;
e{ akrudclr ccf;fn-egt od oo:#000}. bcgon-oo:#99c otwih:bl;clr 000;
v{akrud clr ccc;clr 000;
v bcgon-oo:#ccc etain ih;clr 000;
m fot ih;bre:0x}h wdh 0p;bcgon-oo:#ccc odr p;hih:1x oo:#000}mt ae"OOS otn=NIDXNFLO,ORHV"/igbre=0 r=/rjc/hif.h?PP96F4D2-12A6-0A0AF2 l=PPLg"//r
t>t ls=e>ul aet ls=

If i removed the www. from the link it worked fine.
I called the webhost provider - and he claimed they both worked for him.
I let some friends check it out, and also for them it worked okay.

Somehow this must be a local problem, however i am really unable to address it.
0
 

Author Comment

by:Nielsdv
ID: 24259539
The script still fails on my webhost , on everyone who i let it check it gave the outcome without the 1st TEST in the array.
0
 
LVL 5

Expert Comment

by:cdaugustin
ID: 24259565
hey nields, i see the php info page just fine, the only thing interesting there is the version of the php (latest from feb 2009)(5.2.9) my version is a bit older (5.2.4) and the script runs fine with me. I also looked in the change log for 5.2.9 and there was some work done on explode and array_intersects (altough dunno if they are the cause).

either way run some simple code like the attached and see whats the output
$firstArray = array("a1", "a2", "a3", "a4");
$secondArray = array("a3");
$thirdArray = array("b1", "b2", "a3");
 
print_r(array_intersect($firstArray, $secondArray));
echo "<br>";echo "<br>";
print_r(array_intersect($thirdArray, $secondArray));

Open in new window

0
 

Author Comment

by:Nielsdv
ID: 24259585
Output works how it should :

Array ( [2] => a3 )

Array ( [2] => a3 )

- maybe it has something to do with explode but i can't really see what
0
 
LVL 5

Accepted Solution

by:
cdaugustin earned 1000 total points
ID: 24259623
I think its the

$string = explode("\n",$string);


dunno how you edit your files but if you do it in windows the end line is \r\n so basically after your explode your left with TEST\r  and ofcourse that wont match with TEST


try the following

$string = str_replace("\r\n", "\n", $string);
$string = explode("\n",$string);

and let me know if that was it
0
 

Author Comment

by:Nielsdv
ID: 24259653
That is the answer to my problem, it works correctly now.

Thanks allot! for your help :)

It's really appreciated.
0
 

Author Comment

by:Nielsdv
ID: 24259700
I've truly learned my lesson. Don't use notepad - not even when you're too lazy to open Eclipse

>_<
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question