Skip to content

Instantly share code, notes, and snippets.

View lesterchan's full-sized avatar
🥞
Full Stack Engineer

Lester Chan lesterchan

🥞
Full Stack Engineer
View GitHub Profile
@lesterchan
lesterchan / balanced_parentheses.php
Created April 3, 2014 15:07
Use a stack to check whether the parentheses in a string is balanced ()[]{}
<?php
function is_pair($open, $close)
{
if($open == '(' && $close == ')') return true;
elseif($open == '{' && $close == '}') return true;
elseif($open == '[' && $close == ']') return true;
return false;
}
function balance($test)
{
@lesterchan
lesterchan / get_primes.php
Created April 3, 2014 15:11
Write a function which, taking in a positive integer n as input, returns an array of all primes lower than n.
<?php
/*
Sample expected output:
getPrimes(5); ⇒ array(2, 3)
getPrimes(10); ⇒ array(2, 3, 5, 7)
*/
function getPrimes($n)
{
if ($n <= 0) throw new InvalidArgumentException("positive integer required");
@lesterchan
lesterchan / factorial_iterative_recursive.php
Created April 3, 2014 15:20
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. There are 2 ways to archive it, by iterative function or by recursive function.
<?php
function factorial_iterative($n)
{
$k = 1;
for($i = $n; $i > 1; $i--)
{
$k *= $i;
}
return $k;
@lesterchan
lesterchan / countries.php
Created September 22, 2014 10:15
Countries In PHP Array
<?php
$countries = array( 'Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia and Herzegowina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Indian Ocean Territory', 'Brunei Darussalam', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands', 'Colombia', 'Comoros', 'Congo', 'Congo, the Democratic Republic of the', 'Cook Islands', 'Costa Rica', 'Cote d\'Ivoire', 'Croatia (Hrvatska)', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islan
@lesterchan
lesterchan / ip_address.php
Created September 30, 2014 09:19
Get IP Address
<?php
function get_ip() {
foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
if ( array_key_exists( $key, $_SERVER ) === true ) {
foreach ( explode( ',', $_SERVER[$key] ) as $ip ) {
$ip = trim( $ip );
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false ) {
return $ip;
}
}
@lesterchan
lesterchan / social_media_colors.css
Last active August 29, 2015 14:15
Social Media Colors
.color-pinterest {
color: #cb2027;
}
.color-facebook {
color: #3b5998;
}
.color-twitter {
color: #00aced;
}
.color-vimeo {
@lesterchan
lesterchan / delete_wordpress_revisions.php
Last active August 29, 2015 14:15
Delete WordPress Revisions
<?php
/**
* Delete revisions from WordPress
*/
define( 'WP_USE_THEMES', false );
if( ! empty( $_SERVER['WP_DIR'] ) ) {
require( $_SERVER['WP_DIR'] . '/wp-blog-header.php' );
} elseif( is_file( 'wp-blog-header.php' ) ) {
require( 'wp-blog-header.php' );
} else {
@lesterchan
lesterchan / hhvm.conf
Created April 24, 2015 06:50
HHVM with PHP-FPM fallback
location ~ \.(hh|php)$ {
proxy_intercept_errors on;
error_page 502 = @fallback;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
@lesterchan
lesterchan / wp-includes_user.php
Created June 22, 2013 03:09
WordPress hacking attempt at getting password written to wp-content/plugins/.htaccess
<?php
function wp_signon( $credentials = '', $secure_cookie = '' ) {
if ( empty($credentials) ) {
if ( ! empty($_POST['log']) )
$credentials['user_login'] = $_POST['log'];
if ( ! empty($_POST['pwd']) )
$credentials['user_password'] = $_POST['pwd'];
if ( ! empty($_POST['rememberme']) )
$credentials['remember'] = $_POST['rememberme'];
}
@lesterchan
lesterchan / 2_missing_numbers.php
Created July 5, 2013 09:57
You have a array of a million integers. They are ordered 1,2,3,4,5... all the way to 1,000,000. I shuffle the array and I remove two of the numbers. Write a function (in any language of your choice) that takes the array of 999,998 numbers and efficiently determines which two numbers between 1 and 1,000,000 have been removed.
<?php
// Max Integer
define('MAX', 1000000);
// Create An Array Till MAX
$original = range(1, MAX);
// Shuffle The Array While Preserving Keys
$shuffle = $original;
shuffle_assoc($shuffle);