Strcmp outputs a negative value when comparing files - php

What I need to know is why my strcmp outputs -11?
I have checked both files, the result is the same as
Magnum 23-08-2011 1st 0006,2nd 0661,3rd
6358,S:2359,5341,3075,4048,3720,8648,2774,7109,6360,1422,C:6149,0303,4841,3606,0076,2648,6736,7978,5986,7051
Here is my code:
$checkfile2 = fopen("/var/www/html/magnum/check.txt","r");
fclose($checkfile2);
$checkfile3 = fopen("/var/www/html/magnum/result/".$Current."Magnumresult.txt","r");
fclose($checkfile3);
echo strcmp($checkfile2, $checkfile3);
Thank you, hope you guys reply to me soon.

fopen() function return a file pointer resource not string. So you are getting the error.
If you want to compare contents of each file then use file_get_contents() function to get the contents of each file.

Related

array_diff() in PHP is giving unexpected information

This is the contents of my file spectrum_info.txt:
raid,spectrum/raid_info.txt
rbid,spectrum/rbid_info.txt
rcid,spectrum/rcid_info.txt
rdid,spectrum/rdid_info.txt
reid,spectrum/reid_info.txt
When I load this file data using below code
$s_types=file('spectrum/spectrum_info.txt');
$i=0;
foreach($s_types as $s_type)
{
$s_arrfields = explode(',', $s_type);
$s_names[] = $s_arrfields[0];
$sf_names[] = $s_arrfields[1];
}
I put file_names information from the above file into $sf_names array.
Manually I created one other array having same data:
$f_temp=array("spectrum/raid_info.txt",
"spectrum/rbid_info.txt",
"spectrum/rcid_info.txt",
"spectrum/rdid_info.txt",
"spectrum/reid_info.txt",
"spectrum/rfid_info.txt",
"spectrum/rgid_info.txt",
"spectrum/rhid_info.txt");
print_r($f_temp);
echo"<br/>";
print_r($sf_names);
echo"<br/>";
$result=array_diff($f_temp,$sf_names);
print_r($result);
Both arrays are having same data. Still array_diff() shows all array elements as being different.
I'm trying this because, when i used $sf_names file information to read data from that particular file I get the below error.
Warning: file(spectrum/raid_info.txt ): failed to open stream: Invalid argument
Please help me regarding array_diff() as how to change data loaded from a file equal to manually created array. It can be useful in solving the above error. Thanks.
The values returned by file(), by default, contain the newline character at the end of each line. The FILE_IGNORE_NEW_LINES flag can be used to drop the newline from each value.
$s_types = file('spectrum/spectrum_info.txt', FILE_IGNORE_NEW_LINES);
For full details, see http://php.net/manual/en/function.file.php

php, string value, length and integer value of a variable don't matche

I am getting a variable from a socket. After reading the respond I am trying to convert the value to a number. if I print the respond it looks correct however when I convert it to number using floor() it doesn't give me the right answer. I also tried to print the length of the variable and it is still not working as it suppose to: This one is for value :185
echo("**************** ".floor($res[0]));
echo "################### $res[0]";
echo "------------- ".strlen($res[0]);
output:
**************** 1################### 185------------- 12
I have also tried stripslashes, trim and also ereg_replace('[:cntrl:]', '',$res[0])
Please try the following: intval($res[0])
You can try also with:
$res = (int)$reg[0];
Hope this helps.
Bye
Ok I found the problem. I saved the value in a file and opened the file using notepad++. I saw the following character in between my string:
SOH,NULL, and bunch of other non character values
What I am assuming is PHP automatically ignore the ASCII values are not show able on the screen (less than 21Hex).

Reversing the PHP file() function

I converted a .php file to an array using the file() function.
Now I modified the array and I want to put all it contents back to the file (keeping line endings)...
Someone can tell me if there is a function that does the opposite of file()?
I want to bypass the annoying "array to string conversion" way...
Thanks in advance!
Use file_put_contents() and implode():
file_put_contents('/your/file.php', implode(PHP_EOL, $fileArray));

How to format js object to php array?

So I found some answers on how to do this, but none of them actually worked, i.e. json_decode(). This is what I did:
I created js object/array
Then I passed it to php file via Ext.Ajax.Request as JSON.stringify(js object)
Now in my php I see the result of that string as follows: ["James;","George;"]
I want to get it as an php array like (James, George). Any easy way to do this or I have to remove unnecessary parts manually?
OK, I was looking at this problem for a while and finally got the answer.
Inside php, I needed to add json_decode(stripslashes($scenarios)), where $scenarios = ["James;","George;"].
Code: ($scenarios is sent from js file via Ajax using JSON.stringify(js object))
<?php
$scenarios = empty($_GET['scenarios']) ? false : $_GET['scenarios'];
// more code for validation
$arr = json_decode(stripslashes($scenarios));
?>
Now $arr will become regular php array.
Use html_entity_decode function

PHP read, stored php var in text file

I'm trying to figure out how to stored php values in a string/file.
I have a text file with "var1=foo&var2=foo2" etc in it, is there a way to read the values?
You could use file_get_contents() to open the file and parse_url() to parse the contents into an associative array.
$file = file_get_contents('file.txt');
parse_str($file, $params);
CodePad.
Or if you can change it, theres always serialize() and unserialize()

Categories