I've been searching for this question, but I think it haven't been asked yet.
I have got a problem with reading via fgetc() from my file. When I need to read the apostrophe, the program replace it with ???, so I am not able to add apostrophe to my array. Here's the code (I cut it - so there's no array adding):
$file = fopen("file.txt", "r");
$read_c;
while(!feof($file)) {
while(ctype_space($read_c = fgetc($file)));
echo $read_c . " ";
}
fclose($file);
Now, when there's an apostrophe in the text in the file
’a’
I get in the terminal:
? ? ? a ? ? ?
The strange thing is, when I put in the code
echo $read_c
Instead of
echo $read_c . " "
The output is given correctly
’a’
Thank you all for you help.
Related
I am new to the world of coding and learning PHP these days. For almost one week of research on this issue , I have almost given up on this issue. Hope to get some good insight on it from the experts.
Problem :- I have a CSV file which has information about servers. for Example :
ClientId,ProductName,Server,ServerRole,Webserver,DatabaseName
001,abc,Server1,Web,Webserver1,,
001,abc,Server2,Dabatase,,Database1
001,abc,Server3,Application,,,
002,abc,Server4,Web,Webserver2,,
002,abc,Server5,Database,,Database2,
I created a HTML page which has a simple html form which takes a server name as an input and invokes the commands written in a page called "search.php". I am able to save the user input from index form to a variable fine . But here is the real problem. I want to search that variable against this CSV file , find the client name ( column 1) related to that server ( which should be matched from column 3 ) and then , print all the lines for that client. For e.g. if I input "Server3" , I should get the first three lines as output in a table form.
I have used fgetcsv() , fgets() etc. but I dont seem to crack this. So far , the closest I have reached is printing all the lines which contain the input text (and that too not in a table form). Any help to resolve my problem would be much appreciated.
Here is my code so far:
<?php
$name = $_POST["search"];
echo "You have searched for the server <b>$name</b>";
$output = "";
$fp = fopen("D:\VMware\DSRM\Servers\Servers.csv", "r");
// Read file
$txt = fgets($fp);
while ( !feof( $fp ) ) {
// Search for keyword
if ( stripos( $txt, $name ) !== false ) {
$output .= $txt.'<br />';
}
$txt = fgets($fp);
}
echo $output;
?>
What about regex?
$input_lines = file_get_contents("theCSV");
$server = "Server3";
preg_match_all("/(\d+).*(".$server.")(.*)/", $input_lines, $clientid);
preg_match_all("/(". $clientid[1] .".*)/", $input_lines, $output_array);
Var_dump(output_array[1]);
In theory this should work :-)
This question already has answers here:
Append to next line in text file using php [duplicate]
(3 answers)
Closed 7 years ago.
I'm working with a txt file and PHP and I need to save data into this txt file, save and open the file is not a problem with file_get_contents and file_put_contents but I have a doubt please help me if you can:
I present the info in this order:
issue info 1
issue info 2
issue info 3
as you can see I show to the user the issues in different lines, but that works when I display that info in the browser but if I open the txt file it shows this:
issue info 1issue info 2issue info3
all in the same line, how can I do in order to save the info into the txt file with the order:
issue info 1
issue info 2
issue info 3
thanks in avance, this is my code by the moment:
$filename = "C:/Users/usuario/Videos/Desktop/prueba.txt";
if(file_exists($filename)) {
$filestring = file_get_contents($filename, NULL, NULL);
$convert = explode("\n", $filestring);
for ($i=0;$i<count($convert);$i++)
{
echo $convert[$i]. "</br>";
}
echo "</br>";
$filestring.= "issue 1"."</br>";
file_put_contents($filename, $filestring);
echo "</br>";
}
else{
die("ese file no existe");
}
Use \n in your code, see this example:
<?php
$string = '';
for($i = 1; $i <= 3; $i++) {
$string .= 'issue info ' . $i . "\n";
}
file_put_contents('11.txt', $string);
?>
Output:
issue info1
issue info2
issue info3
Why not just add a new line characters to your strings (before and after the BR tags)?
"\r\n"
This way, you'll get the line breaks in HTML and in text files.
Also, make sure to double-quote the newline characters, or else PHP will not evaluate them properly.
I get the job done to parse data from target file in binary form with the help of stackoverflow's friends.
<?php
$handle = fopen('data', 'rb');
fread($handle,64);
while (!feof($handle)) {
$bytes= fread($handle,32);
print_r(unpack("La/fb/fc/fd/fe/ff/fg/fh",$bytes));
echo "<br/>";
}
echo "finish";
fclose($handle);
?>
I got the result ,one last bug remains here that can't solve myself.
1.why unpack(): Type L: not enough input, need 4, have 0 ?
2.how to fix it?
Change your loop to:
while ($bytes = fread($handle, 32)) {
print_r(unpack("La/fb/fc/fd/fe/ff/fg/fh",$bytes));
echo "<br/>";
}
feof($handle) doesn't become true until after you've tried to read at the end of the file.
So you're performing an extra fread(), which returns false, and then trying to unpack an empty byte string.
Has anyone got any idea to why doesn't the following work ?
$file = 'images/thumbs/1%20-%20Copy.jpg';
if(!file_exists($file)){
die('NOT THERE');
}
echo 'Yes its there.';
The problem is with the spaces. I have checked the file exists,dbl checked n triple checked im going nuts. :(
Help
file_exists works on the file system and not via HTTP. So %20 will not be recognized as space but literally as %20; use spaces instead:
$file = 'images/thumbs/1 - Copy.jpg';
$file = rawurldecode('images/thumbs/1%20-%20Copy.jpg');
try these two
$file = 'images/thumbs/1\ -\ Copy.jpg';
$file = 'images/thumbs/1 - Copy.jpg';
My objective is to look for Company key-value in the registry hive and then pull the corresponding Guid and other keys and values following it. So I figured i would run the regedit export command and then parse the file with php for the keys I need.
So after running the dos batch command
>regedit /E "output.txt" "HKLM\System....\Company1"
The output textfile seems to be in some kind of UNICODE format which isn't regex friendly. I'm using php to parse the file and pull the keys.
Here is the php code i'm using to parse the file
<?php
$regfile = "output.txt";
$handle = fopen ("c:\\\\" . $regfile,"r");
//echo "handle: " . $file . "<br>";
$row = 1;
while ((($data = fgets($handle, 1024)) !== FALSE) ) {
$num = count($data);
echo "$num fields in line $row: \n";
$reg_section = $data;
//$reg_section = "[HKEY_LOCAL_MACHINE\SOFTWARE\TECHNOLOGIES\MEDIUS\CONFIG MANAGER\SYSTEM\COMPANIES\RECORD11]";
$pattern = "/^(\[HKEY_LOCAL_MACHINE\\\SOFTWARE\\\TECHNOLOGIES\\\MEDIUS\\\CONFIG MANAGER\\\SYSTEM\\\COMPANIES\\\RECORD(\d+)\])$/";
if ( preg_match($pattern, $reg_section )) {
echo "<font color=red>Found</font><br>";
} else {
echo "not found<br>";
echo $data . "<br>";
}
$row++;
} //end while
fclose($handle);
?>
and the output looks like this....
1 fields in line 1: not found
ÿþW�i�n�d�o�w�s� �R�e�g�i�s�t�r�y�
�E�d�i�t�o�r� �V�e�r�s�i�o�n�
�5�.�0�0� � 1 fields in line 2: not
found
1 fields in line 3: not found
[�H�K�E�Y��L�O�C�A�L��M�A�C�H�I�N�E�\�S�O�F�T�W�A�R�E�\�I�N�T�E�R�S�T�A�R�
�T�E�C�H�N�O�L�O�G�I�E�S�\�X�M�E�D�I�U�S�\�C�O�N�F�I�G�
�M�A�N�A�G�E�R�\�S�Y�S�T�E�M�\�C�O�M�P�A�N�I�E�S�]�
� 1 fields in line 4: not found
"�N�e�x�t� �R�e�c�o�r�d�
�I�D�"�=�"�4�1�"� � 1 fields in line
5: not found
Any ideas how to approach this?
thanks in advance
Try adding /A to REGEDIT command like this to produce compatible output:
REGEDIT /E /A "output.txt" "HKEY_LOCAL_MACHINE\System....\Company1"
I know there is a Perl library for this:
Parse::Win32Registry
Making a PHP class from it shouldn't be too difficult though. There's also a PECL extension for PHP that will parse Perl code:
http://devzone.zend.com/node/view/id/1712
Regular expressions work fine with unicode. Are you getting a specific error message?
From Windows XP the Regedit export is Unicode and therefore 2 bytes. You'll see this if you open up the export in notepad. I'm not sure older versions of php are able to handle unicode files.
Is there no way you can read the specific key you need? Through another tool etc. That would be a much more straighforward approach.