PHP hexadecimal matrix map - php

I have hexadecimal content saved inside a string, however I would like to get a value that is located at a specific address (ex. 0xD3)
I have the following code to read the .bin file and show it's content:
$handle = #fopen($file, "r");
if ($handle) {
while (!feof($handle)) {
$hex = bin2hex(fread ($handle , 16 ));
print $hex."\n";
$input_output .= $hex;
}
fclose($handle);
}
I am looking for a way to map the addresses in order to find the values I want to.
Ideas ? thanks in advance

Related

Read first 3000 bytes of a text file in php

Solved! I used an existing json file that I was using to display diagrams on the page.
I have a 2000 line text file. I want to read in the first 3000 bytes to a php variable. This works, but only at the cost of reading in the entire text file:
$little_diagrams = ('assets/diagrams.txt'); $mason = file($little_diagrams);
I tried this, but it doesn't work. Any ideas on why?
$little_diagrams = file_get_contents("assets/diagrams.txt", NULL, NULL, 0, 3000);
$mason = file($little_diagrams);
The trouble is that I have to process lines in "assets/diagrams.txt" such as:
2833|6979|Poloskov|||Nikolayev|Igor|2272|1n3rk1/3p1ppp/5q2/2p1P3/2B2P2/r2Q2P1/1b2N2P/1R3K1R|
2832|6979|Poloskov|||Nikolayev|Igor|2272|r2qk2r/1b1p1ppp/n4b2/2pN4/2B1P3/8/PP3PPP/R2QK1NR|
2831|6978|Nikolayev|Igor|2272|Buturin|Vladimir (IM)|2405|r3r1k1/1ppb1pp1/3p1n1p/2nP4/p3P3/4NP2/PPBN1KPP/R3R3|
2830|6978|Nikolayev|Igor|2272|Buturin|Vladimir (IM)|2405|r2qr1k1/1ppb1pp1/p1np1n1p/8/3PP3/4NN2/PPB2PPP/R2Q1RK1|
2829|6977|Nikolayev|Igor|2272|Tabatadze|Tamaz|2288|2rqk2r/4bp1p/p1n1b3/3pP3/Pp1P2p1/1P3p2/1B2NPPP/2RQNRK1|
2828|6976|Lutsko|Igor|2307|Nikolayev|Igor|2272|6r1/1pp2p1k/p2p3p/2bP4/2P2r2/1P4NP/P2R1P1K/5R2|
NEW CODE: (doesn't work, no diagrams are displayed at http://communitychessclub.com/ left column bottom)
$filename = "assets/diagrams.txt";
$handle = fopen($filename, "r");
$little_diagrams = fread($handle, 3000); //<<--- as per your need
fclose($handle);
$X = 5000; $line = 0;
foreach($little_diagrams as $line) {$X++; if ($X >= 5040) {break;} $token = explode("|", $line); //etc
}
<?php
$filename = "c:\\files\\yourfile.txt";
$handle = fopen($filename, "rb");
//$little_diagrams = fread($handle, filesize($filename));
$little_diagrams = fread($handle, 3000); //<<--- as per your need
fclose($handle);
?>
Try above code and read this fread function documentation
I hope this will help also see the example code Example #2 Binary fread() example
fread takes two arguments
string fread ( resource $handle , int $length )
second is length part
length bytes have been read as per documentation
fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met:
UPDATED BELOW
Note:
You can use file_get_contents() to return the contents of a file as a string.
change second argument to FALSE from NULL then try hope it will work
so in my opinion correct code will be like below take a try
<?php
//however working with null also
$file_content =file_get_contents('demoTest.txt',FALSE,NULL,0,3000);
echo 'File Size: '.filesize('demoTest.txt');
echo '<br/> CONTENT HERE<br />'.$file_content;
echo '<br /><br />String Length: '.strlen($file_content);
?>
read the documentation at here for file function

PHP opening a large file

What's the best way to read a .TXT file (The file size is 225mb). I want to open the file and loop thru it and find information via REGEX.
Example data in the file :
00424333060001410100100BILLLLOYD BRRUSSELL & 12675 MAKALISO AVE WEST WORKS TOWN KS 23456-1035 3341310350630200500004200000001887800001789IWD QM1214200400003367250001799900001287IWD QM 000000000000000000000000000000 000000000000000000000000000000
The problem I am having is the file taking forever to open. And to search thru takes a while. My loop could have 75 items I need to search.
$name2 = "BILLLLOYD BRRUSSELL ";
$RE21 = "/[0-9]{23}.$name2/";
$file = fopen("MYFILE.TXT", "r");
while(!feof($file)){
$line = fget($file);
for ($row = 0; $row = 75; $row++{
$name2 = data i am getting from another file...;
$RE21 = "/[0-9]{23}.$name2/"; //Not sure if this works!!
$a = preg_match($RE21, $line, $matches);
foreach($matches as $x => $x_value) {
I will $x_value and store it.} //$x_value should be 00424333060001410100100BILLLLOYD BRRUSSELL
} //foreach
} //for
}//while
fclose($file);
Maybe you should try a different approach and use command line grep? Generate the regexps from your "another file" and the execute a grep command using your generated pattern and the file you want to search?
Use the -o flag to only get your matches from the result
You can read it line by line:
$handle = fopen("bla.txt", "r");
while (($buffer = fgets($handle, 4096)) !== false) {
// ...
}
fclose($handle);

Strip URL's from file using PHP

I have a list of URL's I would like to clean back to their root domain (and sub domains), so far I have this which is fairly simple and works how I need it to:
echo parse_url('https://app.ffgx.com/fdgdfg', PHP_URL_HOST) . '<br />';
echo parse_url('https://www.fgigo.com/fgdfg', PHP_URL_HOST) . '<br />';
However I have a large list of over 200 URL's, so adding each URL like this will be very time consuming.
My question is, how can I upload this script to my server and include an upload button which will allow me to upload a list of URL's in txt or csv format; it will then run through the list and present me with all the stripped URL's?
Something like this with txt file. Where inputfile.txt contains your url's that should be 1 in a line.
https://app.ffgx.com/fdgdfg
https://www.ffgx.com/fdgdfg
Then Store all the extracted domain in array, $url_list. Then depend on you what you will do with array data.
$url_list = array();
$handle = #fopen("/path/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$url_list[] = parse_url($buffer, PHP_URL_HOST);
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
If your URL's in the file will only be starting with "https" or "http", then you read the text file and by using str_replace(), all the url's can be removed / replaced. And then again you can write the new replaced content into the file.

simplexml_load_file odd behavior

I have a file (sites.txt) that has two entries:
http://www.url1.com/test1.xml
http://www.url2.com/test2
Whenever I execute the below PHP code, the 'url1.com' returns false, and the 'url2.com' is loaded into $xml. The odd part is that if I interchange the URLs in the file, i.e.
http://www.url2.com/test2
http://www.url1.com/test1.xml
It loads both. Both URLs are valid XML documents. Why does the order matter here?
Code:
if (file_exists('sites.txt')) {
$file_handle = fopen("sites.txt", "r");
while (!feof($file_handle)) {
$site = fgets($file_handle);
$xml[] = simplexml_load_file($site);
}
fclose($file_handle);
}
try changing your text file to a csv then explode contents of the file on the delimiter:
http://www.url1.com/test1.xml,
http://www.url2.com/test2
$file = fopen("sites.txt", "r");
$files = explode(",", $file);
Sounds like there are some other things going on in addition to this but that you may have that sorted out...

PhP - How to place different strings into repeating embedding text?

OK so let's say I have a .txt file with the following lines:
Mark
Jane
Ann
How could I make a php code which reads these names line by line and then saves them in a new text file like this:
Hi my name is Mark, what's yours?
Hi my name is Jane, what's yours?
hi my name is Ann, what's yours?
P.S - This is a simplified version of my problem, I actually have a list of URLs that I need to sorround by some XML code and then stack those one on top of another, so it turns out to be one big XML file. The surrounding blocks of XML text are static, they don't change, the only thing that should change is the part where the URL needs to be inserted, of course.
How about this: (result.txt contains your result, and input.txt contains your names)
<?php
$filename = "result.txt";
if (!$handleRes = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
$handle = #fopen("input.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
fwrite($handleRes, "Hi my name is ".chop($buffer).", what's yours?\n");
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
$aString = file_get_contents("text.txt"); // get text from file
$aString = explode("\n", $aString); // split this text with new line character
$aString = array_map('trim',$aString); // remove spacing between left and right
foreach($aString as $index => $strString){ // browse each line
$aString[$index] = "Hi my name is $strString, what's yours?"; // change to new format
}
$aString = implode("\r\n", $aString); // join array to string
echo $aString;
Open the file, and then read.
$names = file("names.txt");
foreach ($names as $name) {
printf("Hi My name is %s, What's yours?\n", $name);
}

Categories