PHP Replace binary file at hex address [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to modify a binary file server side of aprox. 700kb to change an url in it than save it.
My first idea was to use bin to hex and preg_replace to replace url in binary.
The adress for the url in binary is always the same, but i need to change it every time my function is called.
Is there a better/faster way to do this ?
Example:
somesite.com/api***
I want to replace *** with some numbers from a var for example.
*** is between hex adress 00010edb-00010edd
Thanks!

If the string is at fixed position, you can write data directly:
$position=hexdec("00010edb"); // You have to pre-calculate it once
$data="some data"; // Replacement
if ($f=fopen("your_file", "r+")) {
fseek($f, $position);
fwrite($f, $data);
fclose($f);
} else {
echo "Can't open file";
}

Related

Content of File PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a file that i opened:
$linie = file("hasla-kopia.txt"); (It's in Polish)
And i have a problem with getting content of a file where every element is in the next line.
This is how my file looks like:
enter image description here
I want the exact output on my php file. I know you can do it by nl2br() function but it's really important for me to make every element in a different HTML tag because i want my final output to be:
1000111011111 in decimal system is: some number
and I don't know how to do the content on separate lines, change to int and write this text after it. Can someone please help me out?
$lines = file('hasla-kopia.txt');
foreach ($lines as $line_num => $line) {
echo $line."<br>";
}
that allows reading line by line and each line is the variable $line
you can use
$file = file_get_contents("hasla-kopia.txt");
$em = explode("\n" , $file);
for($i=0;$i<count($em);$i++){
echo $em[$i];
};

PHP - Trying to delete some elements of an array then repost all the content to a file. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
if(isset($_POST["delete"])){
$file_name = 'ids';
touch($file_name);
$handle = fopen($file_name, 'r+');
$stream = file_get_contents('ids');
$pieces = explode(" ", $stream);
unset($pieces[0]);
unset($pieces[1]);
unset($pieces[2]);
file_put_contents("ids", "\n" );
file_put_contents("ids", $pieces);
fclose($handle);
}
So I'm sure this is wicked messy and a terrible way to accomplish my goal but I am learning PHP for school and this is giving me a lot of issues.
When I press my button I want to take the content I have in 'ids', change it by deleting the strings located in the array at 0,1,2 and then rewrite the new content to 'ids'. Any help would be appreciated.
I'm not sure where you are getting stuck, but I would guess it is regarding the "re-assembly" process, so I'll fix that.
Untested Code:
if(isset($_POST["delete"])){
$file_name='ids';
touch($file_name);
$handle=fopen($file_name, 'r+');
$stream=file_get_contents($file_name);
$content=str_replace(" ","\n",explode(" ", $stream,4)[3]); // explode into a max of 4 elements, access the last element, replace spaces with `\n`
file_put_contents($file_name,$content);
fclose($handle);
}

PHP: Replace php code in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes
How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as #OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.
$string = str_replace("{first}","if($x == 1)",$string);
$string = str_replace("{second}","} else {",$string);

line break conversion from html to php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have html code loaded in array $gh[0] and i am coonverting it to plain text
if i compare with same text loaded into new variable it doesn't works
can someone help with the issue, what should be exact value in $a to match the content in $gh[0]
i want comparsion to be working , what will be output if $gh[0] is converted into plain text especially line break
<?php
$gh[0]="Net inventory (used in)/from<br>Production Activities"
$fg=$gh[0]->plaintext;
$a="Net inventory (used in)/from Production Activities"
if($a == $fg)
{
echo "match";
}
else
{
echo "No match";
}
?>
What about just stripping all elements from the input?
$gh[0]= strip_tags($gh[0]);
If you want to know what the value (of $fg) is, why don't you check it?
There's echo, print_r and var_dump.

File Writing in php without appending [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Following is my piece of code which writes to file.
<?php
$fileWrite = fopen("c.txt", "w+");
for($i=0;$i<5;$i++) {
$bytes = fwrite($fileWrite, $i);
}
fclose($fileWrite);
I am getting 01234. It means , pointer is appending to last location, I don't want to append data. Instead need to write 4 in the file.
Then you need to ftruncate the file before writing to it:
ftruncate($fileWrite, 0);
$bytes = fwrite($fileWrite, $i);
This is obviously pretty pointless to do in a loop, but I expect you know that.
I personally recommend using file_put_contents for this simple task. It's easier to use and will not append to the end of the file (unless specified that way).

Categories