PHP : Weird results with explode and array_intersect

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

NielsdvAsked:
Who is Participating?
 
cdaugustinConnect With a Mentor Commented:
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
 
cdaugustinCommented:
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
 
NielsdvAuthor Commented:
$array is a two dimensional array =)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
cdaugustinCommented:
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
 
NielsdvAuthor Commented:
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
 
NielsdvAuthor Commented:
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
 
NielsdvAuthor Commented:
$array[] -> new array entry -> explode is an array result.

  so array in array

   $array[0] => array[0]=> array[1] =>
0
 
cdaugustinCommented:
yup, your right there, i missed the brackets (maybe i shouldnt post stuff half asleep :)) ) let me take another look at it
0
 
cdaugustinCommented:
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
 
cdaugustinCommented:
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
 
NielsdvAuthor Commented:
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
 
cdaugustinCommented:
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
 
NielsdvAuthor Commented:
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
 
NielsdvAuthor Commented:
http://www.nielsdv.nl/project/parser.php <- here is my script ran from, its on PHP5 Environment
0
 
NielsdvAuthor Commented:
okay i just called up my phpinfo , and i was quite suprised with the results :

http://www.nielsdv.nl/project/phpinfo.php
0
 
NielsdvAuthor Commented:
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
 
NielsdvAuthor Commented:
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
 
cdaugustinCommented:
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
 
NielsdvAuthor Commented:
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
 
NielsdvAuthor Commented:
That is the answer to my problem, it works correctly now.

Thanks allot! for your help :)

It's really appreciated.
0
 
NielsdvAuthor Commented:
I've truly learned my lesson. Don't use notepad - not even when you're too lazy to open Eclipse

>_<
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.