fail in adding numeric values to characters on speical characters - php

I have this function:
function dec($dec)
{
$ans="";
for($x=0; $x<strlen($dec);$x++)
{
$tempAscii = ord(substr($dec,$x,1)) - ($x+1)*2; //((int)dec[x] - (x+1)*2)
while ($tempAscii <0) $tempAscii+=256;
echo $tempAscii,", ";
$ans = $ans . chr($tempAscii);
}
return $ans;
}
echo "dec, ", dec($_GET['word']);
for example, those dec results are just fine:
input: as output: _o middle output: 95, 111
input: SHA256 output: QD;*+* middle output: 81, 68, 59, 42, 43, 42
But when I'm trying this input, I got something strange:
input: $#SHA256*!# output: " middle output: 34, 60, 77, 64, 55, 38, 39, 38, 24, 13, 13
I'm not sure if the reason I get this output is because I'm doing a mistake in my function, or there is other reason. Why I got " as the last result? thanks.
EDIT: I'm using firefox to see the results.
EDIT: It seems that <is the problem. It seems that firefox take this as "smaller then"... How can I change it to be just > char without any meaning?

Here is the solution:
as #kyeiti mention before, the browser trying to interpret this string as code.
according to this question I solved this by using the command htmlspecialchars. So now my code is:
echo htmlspecialchars(enc($_GET['word']));
many thanks!

Related

Wrong modulus result on PHP but right result on Python

I got the problem when operating this number on PHP
ex:
72 ** 79 % 3337
When i use
echo 72 ** 79 % 3337 // Result is int(0)
Then i try to split into this one
$num = number_format(72 ** 79, 0, '', '');
echo $num % 3337; // Result is 1069
Then again i try using fmod() and bcmod()
$num = number_format(72 ** 79, 0, '', '');
echo fmod($num, 3337); // Result is 2255
echo bcmod($num, 3337); // Result is 2255
But, the result that i want is 285 and when using Python the answer is right.
Why does this happen? Any Solution?
You are overflowing floating point resolution, so you need to do all the math with bcmath, including the exponentiation:
$num = bcpow(72, 79);
echo bcmod($num, 3337);
Output
285
Demo on 3v4l.org

How can I input a string of comma separated numbers, and then reverses its order and displays the reversed array in C#

I am supposed to use the built-in explode and implode functions in my code.
For example,
if the input is 12, 56, 34, 79, 26
the output should be reverse is 26 79 34 56 12.
and here is my code enter image description here
first, assuming input as string, use
String myInput = "12, 56, 34, 79, 26";
string[] inputs = str.Split(',');
Array.Reverse(inputs);
else if you want to derive your own method, see this great stack over flow anser here

Weight represented with decimal, need to show as just numbers, php

I have the weight grabbed from an xml as 0.800, I want to convert it to 000008. This Eg. 003456 is 345.6 kilograms shows how it is to be represented for heavier weights, could someone give me a heads up as to where I could start with this one?
thanks!
EDIT: I have the solution but does anyone know why it cuts off the last digit? See below:
XML:
<GROSSWEIGHT>345.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = ($shipment->COLLO->GROSSWEIGHT*10); // 6 CHARS
sprintf("%06s", $LINE_WEIGHT)
// output 003450
EDIT: If i follow this, it works:
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT);
// output 000008
AS SOON as I include xml data:
echo $shipment->COLLO->GROSSWEIGHT; // 345.800
echo str_pad($shipment->COLLO->GROSSWEIGHT*10, 6, "0", STR_PAD_LEFT);
// output 003450
Please see pic below:
use *10 and then "pad-left":
echo str_pad(0.800*10, 6, "0", STR_PAD_LEFT); //000008
echo str_pad(345.6*10, 6, "0", STR_PAD_LEFT); //003456
You'd first need to multiply by 10, then have a look at sprintf, especially the padding option.
I'll answer my own question to how I decided to go ahead coding. I used the sprintf solution suggested by simon as it seems a lot cleaner.
XML:
GROSSWEIGHT>0.800</GROSSWEIGHT>
PHP:
$LINE_WEIGHT = (utf8_decode($shipment->COLLO->GROSSWEIGHT)*10); // 6 CHARS
sprintf("%06d", $LINE_WEIGHT);
Thanks everyone!

removing data from regular expression [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse a text file containing image data
Here is my code for getting image data from a text file:
while (!feof($fh)) {
$line = fgets($fh);
$lines[] = $line;
$match1 ="/^[0-9]{1,3},[0-9]{1,3}/";
$match2 = "/[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},(?:,?[0-9]{1,3})*(?:\.[0-9]{1,10})?\b/";
$parts = preg_match($match1, $line, $regs);
$parts2 = preg_match($match2, $line, $regs2);
foreach($regs as $key => $lame) {
$lamer[] = $lame;
}
foreach($regs2 as $key => $lame2) {
$lamer2[] = $lame2;
}
}
The first preg_match gets the coords, and second gets the rgba() data.
I'm trying to put this into a javascript array but I am getting this error:
SyntaxError: too many constructor arguments
I assume it's too much data for the javascript array.
Now I am wondering how or if I can skip data in the array, namely the the coords
that have a rgba with 0 alpha, which would mean that I would have to skip both.
I'm also wondering if I should try to combine the two matches into one to see if that would make it easier, but I'm not sure how to do that.
Here's the data I am working with that is a 300x180 image:
41,6: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
42,6: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
90,35: ( 77, 80, 12, 98) #4D500C62 srgba(77,80,12,0.384314)
91,35: ( 95, 99, 13, 78) #5F630D4E srgba(95,99,13,0.305882)
92,35: ( 96, 99, 31, 90) #60631F5A srgba(96,99,31,0.352941)
93,35: (106,110, 14, 68) #6A6E0E44 srgba(106,110,14,0.266667)
94,35: ( 95, 99, 13, 78) #5F630D4E srgba(95,99,13,0.305882)
Use JavaScript RegExp pattern
^(\\d+),(\\d+)[^#]+#.{6}(?!00)[^(]+\\((\\d+),(\\d+),(\\d+),(\\d*(?:\\.\\d*)?)
Check this demo.

get every 4th hour

I have a time string from 0 to 24. like this
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
Now if it is 2 oclock, I want to select every 4th hour. so from above it will be
2, 6, 10, 14, 18, 22
I am trying to wrap my head around it to get this going
thanks
I have tried this
if(range($hour, 23, 4)){
print_r($hour);echo '<br />';
}
but it still prints all the hours in that array
EDIT
here is the code
$hourlyData->hour is just an hour digit, like 0 or 1 or 2 etc upto 23
foreach($jason->hours as $hourlyData){
if(range($hourlyData->hour, 23, 3)){
// print here to check if the correct time is used
print_r($hourlyData->hour);echo '<br />';
}
}
Why use a "time string" for this? It's built-in. For an array:
$hours = range(2, 23, 4); // get every 4th hour starting from 2
And for a string:
$str = implode(', ', range(2, 23, 4));
Finally, to unlock the magic number elimination achievement:
define('HOURS_IN_DAY', 24);
$str = implode(', ', range(2, HOURS_IN_DAY - 1, 4));

Categories