Skip to content

Instantly share code, notes, and snippets.

View phamquocbuu's full-sized avatar
💻

Buu Pham phamquocbuu

💻
View GitHub Profile
<?php
/* Author: Keyvan Minoukadeh
This script demonstrates how you can set callback functions
to receive the HTTP response as it comes through.
The advantage of this is that you don't have to wait for the whole response
to be returned before you start work on it, you can monitor for certain headers,
start outputting while you're receiving, etc..
I wasn't aware these cURL options were available in PHP,
but noticed them being used by Alan Knowles:
<?php
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
@phamquocbuu
phamquocbuu / set_fullscreen.js
Created October 5, 2013 01:38
JavaScript Set fullscreen
function setFullScreen(elm) {
w = $(document).width();
h = $(document).height();
$(elm).css({
"height" : h,
"width" : w
});
}
@phamquocbuu
phamquocbuu / set_center.js
Created October 5, 2013 01:38
JavaScript Set Center
function setCenter(elm) {
w = $(window).width();
lr = ( w - $(elm).width() ) / 2;
h = $(window).height();
tb = ( h - $(elm).height() ) / 2;
$(elm).css({
"margin-left" : lr,
"margin-right" : lr,
"margin-top" : tb,
"margin-bottom" : tb
@phamquocbuu
phamquocbuu / limit_textarea_text_length.js
Created October 5, 2013 01:39
JavaScript Limit textarea text length
$('textarea[maxlength]').live('keyup blur', function() {
// Store the maxlength and value of the field.
var maxlength = $(this).attr('maxlength');
var val = $(this).val();
// Trim the field if it has content over the maxlength.
if (val.length > maxlength) {
$(this).val(val.slice(0, maxlength));
}
});
@phamquocbuu
phamquocbuu / file_get_contents_curl.php
Created October 5, 2013 01:41
PHP Get file content and handle file not found exception
function file_get_contents_curl($url) {
$data = '' ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
@phamquocbuu
phamquocbuu / email_validation.php
Created October 5, 2013 01:41
PHP Email validation
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
// invalid e-mail address
}
@phamquocbuu
phamquocbuu / set_timezone.php
Created October 5, 2013 01:42
PHP Set Date default timezone at HCMC
date_default_timezone_set('Asia/Ho_Chi_Minh');
@phamquocbuu
phamquocbuu / khong_dau_tieng_viet.php
Created October 5, 2013 01:43
PHP Convert Vietnamese to non-signed Vietnamese
function khongdau($str) {
$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", "e", $str);
$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", "i", $str);
$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", "u", $str);
$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", "y", $str);
$str = preg_replace("/(đ)/", "d", $str);
$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", "A", $str);
$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", "E", $str);