Skip to content

Instantly share code, notes, and snippets.

@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 / get_basename.php
Created October 27, 2013 07:45
解決檔案上傳時,PHP的basename函數不支援中文的方法
<?php
//php自帶的basename函數不支持中文,下面這個方法是最簡單的實現。
function get_basename($filename){
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
@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 / 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 / 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 / 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.
@WenLiangTseng
WenLiangTseng / generate_random_string.php
Last active December 21, 2015 10:19
產生隨機字串
<?php //reference: http://stackoverflow.com/questions/5438760/generate-random-5-characters-string
function get_token( $length ) {
$seed = str_split(
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '';
@WenLiangTseng
WenLiangTseng / chinese_excerpt_solution.php
Created August 21, 2013 03:42
中文的Wordpress摘要,含有HTML的Tag時,可保留HTML標籤且避免砍到HTML標籤的完整寫法
<?php // 參考資料來源 http://stackoverflow.com/questions/1193500/php-truncate-html-ignoring-tags
function memo_desc_excerpt($str) {
$len = 100;
//find all tags
$tagPattern = '/(<\/?)([\w]*)(\s*[^>]*)>?|&[\w#]+;/i'; //match html tags and entities
preg_match_all($tagPattern, $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
//WSDDebug::dump($matches); exit;
$i = 0;