How to solve a missing function argument - php

I am having an issue with my function. I can't seem to figure out why it works one way and not another.
When I go to the html source here http://adcrun.ch/ZJzV and place the javascript encoded string into the function It decodes the string just fine.
echo js_unpack('$(34).39(4(){$(\'29.37\').7($(34).7()-$(\'6.41\').7()-($(\'6.44\').7()*2))});$(\'29.37\').39(4(){3 1=-2;3 5=4(){9(1<0){$.26(\'15://25.22/21/24.20.19\',{14:\'46\',13:{16:18,17:23}},4(40){3 28=38(\'(\'+40+\')\');9(28.12&&1!=-2){45(31);3 8=$(\'<6 48="47"><27 36="#">49</27></6><!--43.42-->\');$(\'6.41 33#35\').57().60(\'59\',\'61\').30(8);8.62(4(){$.26(\'15://25.22/21/24.20.19\',{14:\'50\',13:{63:0,16:18,17:23,58:\'\'}},4(5){3 11=38(\'(\'+5+\')\');9(11.12&&1!=-2){52.51.36=11.12.53}});8.30(\'54...\')})}32{1=10}})}32{$(\'33#35\').56(1--)}};5();3 31=55(5,64)});',10,65,explode('|','|a0x1||var|function|rr|div|height|skip_ad|if||jj|message|args|opt|http|lid|oid|4106|php|fly|links|ch|188|ajax|adcrun|post|a|j|iframe|html|si|else|span|document|redirectin|href|fly_frame|eval|ready|r|fly_head|button|end|fly_head_bottom|clearInterval|check_log|continue_button|class|Continue|make_log|location|top|url|Loading|setInterval|text|parent|ref|margin|css|6px|click|aid|1000'));
But hen I use it like this echo js_unpack($full_code); it fails and gives me the following errors.
Warning: Missing argument 2 for js_unpack(),
Warning: Missing argument 3 for js_unpack(),
Warning: Missing argument 4 for js_unpack(),
Here is my php source that I am using.
//function to extract string between 2 delimiters
function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three);
return $unit;
}
//html source
$html = file_get_contents('http://adcrun.ch/ZJzV');
//extract everything beteen these two delimiters
$unit = extract_unit($html, 'return p}(\'', '.split');
//full encoded strning
$string = $unit;
//the part here ne values ill be inserted
$expression = "',10,65,";
//inserted value
$insertvalue = "explode('|',";
//newly formatted encoded string
$full_code = str_replace($expression,$expression.$insertvalue,$string).')';
//function to decode the previous string
function js_unpack($p,$a,$c,$k)
{
while ($c--)
if($k[$c]) $p = preg_replace('/\b'.base_convert($c, 10, $a).'\b/', $k[$c], $p);
return $p;
}
//return decoded
echo js_unpack($full_code);

I didn't go through all your code, but there is a fundamental difference in your first 2 examples.
This line passes 4 arguments to the js_unpack function:
echo js_unpack( '$(......);', 10, 65, explode( '|', '|............' ) );
This line passes 1 argument to it:
echo js_unpack( $full_code );
I don't know if this is the root of your other problems, but it's a poor comparison to say "it works the first way but not the second way". The Warning is telling you exactly what you need to know: you are missing arguments.
Edit:
Based on your comment, I think you do not understand what is truly going on. You say you "copied the string and placed it in the function". This is incorrect. What you really copied was 1 string, 2 ints, and 1 array. You placed these 4 arguments in your function.
Maybe it helps if you format your functions this way:
echo js_unpack(
'$(......);', // <-- Argument #1 (a long string)
10, // <-- Argument #2 (int)
65, // <-- Argument #3 (int)
explode( '|', '|............' ) // <-- Argument #4 (array)
);
Compare that with:
echo js_unpack(
$full_code // <-- Just 1 argument
);
These are simply not the same signatures. Some PHP functions have default argument values, but this is not the case with js_unpack and it gives you a very clear warning that you are not calling it properly.

Related

php playing tricks when I send an "8" as an argument

I can't understand what the hack is going on with my code))) May be I just need more experience but... Let's get down to business:
I wrote a function which returns an array with some statistic data for my system, all I need is just to specify the arguments as Year, M, D. If I send something like getStatData(2016,07,07) - works fine, But just as i send something with a digit eight - it fails! eg.: getStatData(2016,07,08) Of course I have a file the fn requires, more over It works fine if a specify args as strings ('2016', '07', '08'); But this is ain't cool:D
The function:
function getStatData($y,$m,$d=false){
if(isset($y) && is_numeric($y) && isset($m) && is_numeric($m)){
$m=($m<10)? '0'.$m : $m;
if(isset($d)){
$d=($d<10)? '0'.$d : $d;
$y= 'days/'.$y;
$data = file_get_contents(STATDIR.'/'.$y.'/'.$m.'_'.$d.'.json');
}
else {
$data = file_get_contents(STATDIR.'/'.$y.'_'.$m.'.json');
}
return json_decode($data, true);
}
else return false;
}
Calling...
print_r(getStatData(2016,07,08)); //call with 08
ERROR: file_get_contents(core/statistic/days/2016/07_00.json): failed to open stream: No such file or directory
Numbers with a zero prefix are octal literals, rather than zero padded decimal integers. It seems that invalid octals are silently treated as 0 in PHP 5. You should pass in 7 and 8 in the example you have provided.
In PHP 7 you will get a parse error. E.g. Parse error: Invalid numeric literal in php shell code on line 1.
Pass arguments as string like print_r(getStatData('2016','07','08'));
As a side note use str_pad() function to format month and date to 2 digit value. Instead if isset($d) use if($d)
function getStatData($y,$m,$d=false){
if(isset($y) && is_numeric($y) && isset($m) && is_numeric($m)){
// use str_pad
$m=str_pad($m, 2, '0', STR_PAD_LEFT);
if($d){
// use str_pad
$d=str_pad($d, 2, '0', STR_PAD_LEFT);
$y= 'days/'.$y;
$data = file_get_contents(STATDIR.'/'.$y.'/'.$m.'_'.$d.'.json');
}
else {
$data = file_get_contents(STATDIR.'/'.$y.'_'.$m.'.json');
}
return json_decode($data, true);
}
else return false;
}
print_r(getStatData('2016','07','08'));

Warning: str_repeat(): Second argument has to be greater than or equal to 0

I used this a while back to grab images from something but since I just tried using it again it is giving me this error:
Warning: str_repeat(): Second argument has to be greater than or equal to 0 in C:\inetpub\wwwroot\resource_update.php on line 121
This is the function for what its referring to so if anyone could help that would be great:
function consoleLogProgressBar($current, $size, $unit = "kb")
{
$length = (int)(($current/$size)*100);
$str = sprintf("\r[%-100s] %3d%% (%2d/%2d%s)", str_repeat("=", $length).($length==100?"":">"), $length, ($current/($unit=="kb"?1024:1)), $size/($unit=="kb"?1024:1), " ".$unit);
consoleLog($str, true);
}
Sounds like $length is returning a negative number? You could troubleshoot as follows:
$length = (int)(($current/$size)*100);
var_dump($length);
exit;
If that is in fact the case, then you could wrap it in the abs() function which will always return an absolute value:
$length = (int) abs(($current/$size)*100);
Of course, that is an ugly hack and doesn't solve the real issue. Either way, the first step is determine why $length isn't what you expect it to be.

PHP increase number by one

This is a tricky one: I want to add +1 to this number: 012345675901 and the expected result is: 012345675902. Instead I get: 2739134 when I do this:
echo (012345675901+1);
When I try:
echo ('012345675901'+1);
I get this: 12345675902 which is pretty close to what I need, but it removes the leading zero.
When I do this:
echo (int) 012345675901;
I get 2739133. I also tried bcadd() without success:
echo bcadd(012345675901, 1);
which resulted in 2739134.
I know I am missing something here. I would really appreciate your help!
UPDATE 1
Answer 1 says that the number is octal:
function is_octal($x) {
return decoct(octdec($x)) == $x;
}
$number = is_octal(012345675901);
echo var_dump($number);
The above returns false. I thought I needed to convert this from octal to a normal string but didn't work. I can't avoid not using the above number - I just need to increment it by one.
EDIT 2
This is the correct code:
$str = '012345675901';
$str1 = ltrim($str, '0'); // to remove the leading zero
$str2 = bcadd($str1, 1); // +1 to your result
$str3 = strlen($str); // get the length of your first number
echo str_pad($str2, $str3, '0', STR_PAD_LEFT); // apply zeros
Thank you everyone for your help! The above code produces: 012345675902 as expected
The leading 0 is treating your number as octal.
The leading 0 you need for output as a string, is a purely a representation.
please see the code for explanation.
$str = "012345675901"; // your number
$str1 = ltrim($str, '0'); // to remove the leading zero
$str2 = bcadd($str1, 1); // +1 to your result
$str3 = strlen($str); // get the length of your first number
echo str_pad($str2, $str3, '0', STR_PAD_LEFT); // apply zeros

substr return empty string

i have problem with $length of substr function
my CODE
$string='I love stackoverflow.com';
function arabicSubStr($value,$start,$length=false){
return mb_substr($value,$start,$length,'UTF-8');
}
echo arabicSubStr($string,7);//outputs nothing
echo substr($string,7);//outputs stackoverflow.com
The reason of the problem is:
If length is given and is 0, FALSE or NULL an empty string will be returned.
So, How i can fix the problem?
i won't use strlen($string)
EDITE
I know the reason is because i've defined $length as false
And i am here to know what should i put in $length parameter to avoid this error?
i am trying to put -1 it's returns //stackoverflow.co
Since the reason you're getting an empty string is specified entirely by the content of your question (using 0, FALSE or NULL), I assume you just want a way to get the rest of the string.
In which case, I'd use something like:
function arabicSubStr ($value, $start, $length = -1) {
if ($length == -1)
$length = mb_strlen ($value, 'UTF-8') - $start;
return mb_substr ($value, $start, $length, 'UTF-8');
}
You need to do it this way since there is no sentinel value of length that means "the rest of the string". Positive numbers (and zero) will limit the size to that given, negative numbers will strip off the end of the string (as you show in your question edit).
If you really don't want to use a string length function, you could try a value of 9999 (or even higher) and hope that:
the mb_substr() function will only use it as a maximum value; and
you won't pass in any strings 10K or more.
In other words, something along the lines of:
function arabicSubStr ($value, $start, $length = 9999){
return mb_substr ($value, $start, $length, 'UTF-8');
}
Though keep in mind I haven't tested that, I don't have any PHP environments at my current location.
It's because you have $length set to false as the default parameter for your function, which effectivley means you want it to return a substring of 0 length.
Unfortunately, if you have to set the final parameter (the charset) which I imagine you do, then you have to calculate the length of the string first, so something like:
function arabicSubStr($value,$start,$length=false){
$length = ($length) ? $length : mb_strlen($value,'UTF-8') - $start;
return mb_substr($value,$start,$length,'UTF-8');
}

PHP Find Previous String Position

Is there a way that I can search a variable starting from a given position and find the start position of a string that is in the variable backwards from the given start position.
So for example if I initially do $getstart = strpos($contents, 'position', 0);
I then want to do $getprevpos = prevstrpos($contents, 'previous token', $getstart);
Obviously there is no such function as prevstrpos but I hope you get what I mean.
Example text area (terrible example I now):
Here is an example where I want to find the previous token once I have found the start position of a text string.
you can strrpos( substr($contents, 0, $getstart), 'previous token')
Is there something wrong with strrpos()? If 'offset' is negative: "Negative values will stop searching at the specified point prior to the end of the string."
you can try this. I think it should would for all cases but you should probly test it a bit. Might be a bug here and there but you get the idea. Reverse everything and do a strpos on the reversed string
prevstrpos( $contents, $token, $start )
{
$revToken = strrev($token);
$revStart = strlen($token) - $start;
$revContent = strrev($content);
$revFoundPos = strpos( $revContent, $revToken, $revStart );
if( $revFoundPos != -1 )
{
$foundPos = strlen($token) - $revFoundPos;
}
else
{
$foundPos = -1;
}
return $foundPos;
}

Categories