Skip to content

Instantly share code, notes, and snippets.

@WenLiangTseng
WenLiangTseng / apache_setting_for_nodejs.txt
Created March 1, 2016 03:46
Node.js, Apache, subdomain setting example
// reference: http://stackoverflow.com/a/19785468/2253749
<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
@WenLiangTseng
WenLiangTseng / php_detect_user_country.php
Created February 1, 2016 07:14
PHP 偵測 user 的地理區(國家)
<?php
// reference: http://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
@WenLiangTseng
WenLiangTseng / php_display_n_minutes_ago.php
Created December 27, 2015 02:19
PHP顯示多少時間之前
<?php
// reference: http://stackoverflow.com/questions/18685/how-to-display-12-minutes-ago-etc-in-a-php-webpage
function timeAgo($timestamp){
$datetime1=new DateTime("now");
$datetime2=date_create($timestamp);
$diff=date_diff($datetime1, $datetime2);
$timemsg='';
if($diff->y > 0){
$timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');
@WenLiangTseng
WenLiangTseng / php_sort_array_by_sub-array_value.php
Created February 10, 2014 15:11
PHP 使用子陣列中的值,來搜尋陣列中的元素 PHP Sort Array By SubArray Value
<?php
//Source http://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value
//Use usort function and custom criteria.
function cmp_by_optionNumber($a, $b) {
return $a["optionNumber"] - $b["optionNumber"];
}
//and
@WenLiangTseng
WenLiangTseng / php_count_by_subarray_key_value.php
Created February 10, 2014 15:07
PHP Count by sub-array Key & Value
<?php
//Source http://stackoverflow.com/questions/13329621/count-sub-elements-of-an-array-in-php
function array_count ($array, $key, $value = NULL) {
// count($array[*][$key])
$c = 0;
if (is_null($value)) {
foreach ($array as $i=>$subarray) {
$c += ($subarray[$key]!='');
}
@WenLiangTseng
WenLiangTseng / php_search_subarray.php
Created February 10, 2014 14:59
PHP Search sub-array (PHP搜尋子陣列)
<?php
//source: http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php
function search($array, $key, $value) {
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
@WenLiangTseng
WenLiangTseng / convert_string_to_utf8.php
Created January 3, 2014 09:30
Convert Chinese String to UTF8 code.
<?php
function chineseToUnicode( $str ){
$str = rawurlencode( $str );
$str = str_replace("%", "", $str);
return $str;
}
//Print result
$str = "旴";
@WenLiangTseng
WenLiangTseng / get_basename.php
Created October 27, 2013 07:45
解決檔案上傳時,PHP的basename函數不支援中文的方法
<?php
//php自帶的basename函數不支持中文,下面這個方法是最簡單的實現。
function get_basename($filename){
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
@WenLiangTseng
WenLiangTseng / jQuery_click.js
Created September 3, 2013 03:56
解決 jQuery 的 .trigger('click') 或 .click() 無效,無法觸發的方法
//參考 http://stackoverflow.com/questions/5867370/trigger-click-jquery-not-working
$('a#swaswararedirectlink')[0].click();
@WenLiangTseng
WenLiangTseng / browser_conditional_css.php
Created August 23, 2013 10:18
依據不同的瀏覽器,來給予不同的CSS或JS(browser conditional css/js)
<?php
if(isset($_SERVER['HTTP_USER_AGENT'])){
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if (strlen(strstr($agent,"Chrome")) > 0) {
echo '<style type="text/css">#element{top:1px;}</style>';
}
// 'firefox', etc.