PHP code snippet decode - php

I was looking a craigslist for web dev jobs (I am a beginner).
I came across a Jr. job that wanted me to decode this snippet.
<?php
$f1 = 'e' . 'x' . 'p';
$f1 .= 'l' . 'o' . 'd' . 'e';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
?>
Since I do not know the answer I a a bad candidate for the job, but I would like to understand it if anyone can help me. What I do get is that you are using the string of $f1 to manipulate the array $x. But i see problems with using $argv[0] and I believe that using f2(implode('', $x)) will give a parameter error.
Any help would be greatly appreciated.

Hehe, that was kidna fun. It's the email to their HR department.
<?php
$argv[0] = 'base64_decode.';
$f1 = 'explode';
list($f2,$ext) = $f1('.',$argv[0]);
$x = array('a','H','I',
'r','Y','2',
'x','q','c',
'm','R','l',
'd','k','B',
'l','d','m',
'9','r','b',
'm','93','L',
'm','N','v',
'b','Q','');
echo $f2(implode('', $x)) . "\n";
Not really sure what they were looking for in an answer. Made me think of this though. http://blog.sucuri.net/2013/09/ask-sucuri-non-alphanumeric-backdoors.html

Related

how can i add " to a variable in php?

so I'm trying to make a variable that contains a " sign
like if i had a variable called $x
i want my variable
$y = """ , $x , """
but i can't just write """ so what is the method of adding a " to a variable
im using php 5 but i don't think that's gonna help i mean im justwriting more because this site doesn't allow me to post but i can't explain more and i cant find my answer in another place
and again more writing yada yada , some questions are small so WHY just WHY does it think it's incomplete
You can do it like this
$x =11;
$y = '"""'.$x.'"""';
echo $y;
Result
"""11"""
now i finally figured out what i was doing wrong , i was using , instead of . between the values so the final code is
$y = '"' . $x . '"' ;

Incrementing a string by one in PHP

I am struggling to find a way to increment a specific pattern required. For each new member, they are given a unique ID such as ABC000001. Each new members ID should increment by one. So the second member's ID would be ABC000002. I am using PHP and MySQL to keep track of each member but I have not been able to come up with a way to properly increment using the string format above.
What is the best way to approach this?
As #axiac mentions this is probably not a good idea but it's pretty easy to manage.
$memberid = 'ABC000001';
list($mem_prefix,$mem_num) = sscanf($memberid,"%[A-Za-z]%[0-9]");
echo $mem_prefix . str_pad($mem_num + 1,6,'0',STR_PAD_LEFT);
Split your current member number into the alpha and numeric parts then put them back together bumping the number when you do it. I use this as a function and pass the previous ID and what I get back is the next ID in the sequence.
You can extract only digits using regex to increment and using str_pad for create a prefix :
$memberid = 'ABC000001';
$num = preg_replace('/\D/', '',$memberid);
echo sprintf('ABC%s', str_pad($num + 1, "6", "0", STR_PAD_LEFT));
Possible answer without regex.
Runs through each character and checks if it is a number or not.
Then uses sprintf() to make sure leading 0s are still there.
$str = "ABC000001";
$number = "";
$prefix = "";
$strArray = str_split($str);
foreach ($strArray as $char) {
if (is_numeric($char)) {
$number .= $char;
} else {
$prefix .= $char;
}
}
$length = strlen($number);
$number = sprintf('%0' . $length . 'd', $number + 1);
echo $prefix . $number;
This works for this instance but would not work if the prefix had numbers in it.
just use the PHP increment operator ++
as long as you've sufficient leading zeros in the pattern, then PHP will correctly increment the numeric component
This code:
<?php
$name = 'ABC0000098';
print ++$name . PHP_EOL;
print ++$name . PHP_EOL;
print ++$name . PHP_EOL;
Outputs:
ABC0000099
ABC0000100
ABC0000101
Read more in the PHP docs:
https://www.php.net/manual/en/language.operators.increment.php
aha - just saw there is already a link to a similar example in the comment posted by "axiac" above

PHP repeat action with adding value

I am just learning coding, and couldn't find solution for that:
I'm creating a website that shows pictures that are on its server.
$number = $_POST["NR"];
$picture1 = "{$number}.jpg";
echo '<img src="pictures/' . $picture1 . ' ">';
How can I make it to repeat the echo string, but every time add for "$picture1" calculation +1 ? So, that the maximum count would be 40?
Thanks for helping. I know that it is actually easy, but I still don't know how to do it :)
Assuming that you have images named correctly awaiting this to happen:
$number = $_POST["NR"];
$i = 0;
while ($i < 40) {
$picture = $number + $i.".jpg";
echo '<img src="pictures/' . $picture . ' ">';
$i++;
}
Use a counter and loop. The issue here is that your $number could be anything... For instance if someone passed 500, you would need images named, 500.jpg, 501.jpg, etc. all the way up to 540.jpg. Unless I misunderstood the question.

Make an equation in php

I have a php file where i want to make an equation with the output i get.
It is pretty easy if the line is like this:
<?php echo round(($post['voteup']/($post['voteup'] + $post['votedown'] ))*100); ?>
But i have a php line where it goes like this, and i just can't figure out how to make the equation work:
$str='';
$data = $dbc->query($sql);
if($data!=null && $data->num_rows>0){
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
$str.="<p>".$row['voteup']." + ".$row['votedown'].</p>";
}
It may be a silly question, but this really bothers me.
If you want to add the upvotes and downvotes you would do this -
$newTotal = $row['voteup'] + $row['votedown'];
$str.="<p>" . $newTotal ."</p>";
If you are trying to add $row['voteup'] and $row['votedown'] together you should addition them in a variable first then use it.
$total = $row['voteup'] + $row['votedown'];
$str .= "<p>{$total}</p>";
If you really need to make it inline you can do something like this as well.
$str .= '<p>' . ($row['voteup'] + $row['votedown']) . '</p>';
If you just want to display the "equation" simply fix the error.
$str.="<p>".$row['voteup']." + ".$row['votedown']."</p>";
Or you could actually turn it into an equation:
$str.="<p>".$row['voteup']." + ".$row['votedown']." = ".($row['voteup'] + $row['votedown'])."</p>";

PHP finding extent of common sub-string from start

I have two strings which starts out the same, but at some point are different. I want to find how many characters the two strings share in common.
It seems like strpos is the opposite of this; it assumes that one string is found completely in another, but not necessarily at the starting offset.
I see a lot of questions which ask about finding common substrings when they aren't at the start, or when there are more than two strings, and this seems to complicate things quite a bit. I have just two strings, and they have a common prefix. IS there a PHP function to get the index at which these two strings differ?
You can try using for loop by comparing character by character. Something like this
$str1 = 'MyStringFirst';
$str2 = 'MyStringSecond';
$match_from_left = '';
$count = 0;
$limit = strlen($str1) > strlen($str2) ? strlen($str2) : strlen($str1);
for($i = 0; $i < $limit; $i++){
if($str1[$i] != $str2[$i]){
break;
}
$count++;
$match_from_left .= $str1[$i];
}
echo 'Count: ' . $count . '<br />';
echo 'Match: ' . $match_from_left . '<br />';
Similar question answered here: Find first character that is different between two strings
You can use the inbuilt PHP function similar_text(), however as pointed out below, this does not get the index, only a count of the number of same characters.

Categories