Skip to content

Instantly share code, notes, and snippets.

@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;
@WenLiangTseng
WenLiangTseng / jQuery_load_partial_page.js
Created August 20, 2013 14:24
jQuery讀取(重新載入)部分網頁,不用全畫面重新整理
//程式碼
var url = "http://yourdomain.com/yourpage.html";
$("#elementName").load(url+" #elementName>*","");
//應用
$('#button1').click(function() {
var url = "http:www.your-url.com?ID=" + Math.random(); //create random number
setTimeout(function() {
@WenLiangTseng
WenLiangTseng / date_sorting.js
Last active December 20, 2015 22:09
jQuery 日期排序功能
//reference from http://stackoverflow.com/questions/10636779/jquery-date-sorting
//But this version not working. don't know why.
function parseDate(input) {
var parts = input.match(/(\d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]); // months are 0-based
}
var elems = $.makeArray($(".dateDiv"));
elems.sort(function(a, b) {
@WenLiangTseng
WenLiangTseng / alphabet_selection_list.js
Last active December 20, 2015 14:18
產生出英文字母選擇的清單,以及透過點擊英文字母而僅顯示出相對應的清單
<script type="text/javascript">
jQuery(document).ready(function($){
$('.alphabet').click(function(){
$(this).addClass('active').siblings().removeClass('active');
abc = $(this).data('abc');
$('.xxx').each(function(){
@WenLiangTseng
WenLiangTseng / 0_sorting_list_alphabetically.js
Last active December 20, 2015 14:18
jQuery: sorting lists alphabetically 按照英文字母排序 List 清單
(function($) {
$.fn.listSorter = function(options) {
var that = this;
var settings = {
order: 'asc'
};
options = $.extend(settings, options);
var items = $('li', that).get();
@WenLiangTseng
WenLiangTseng / get_DOM_in_an_iframe.js
Created July 23, 2013 09:13
jQuery 取得在 iframe 裡面的 DOM 元素
jQuery("#myiframe").contents().find("#myContent");
//重點在於加上 contents(),便能搜尋然後抓出 iframe 裡面的內容
@WenLiangTseng
WenLiangTseng / wordpress_search_custom_field.php
Created July 23, 2013 09:11
Wordpress 搜尋自訂欄位(custom field)的函數
<?php
/*
Source: http://www.deluxeblogtips.com/2012/04/search-all-custom-fields.html
*/
global $wpdb;
// If you use a custom search form
// $keyword = sanitize_text_field( $_POST['keyword'] );
// If you use default WordPress search form
@WenLiangTseng
WenLiangTseng / tourney.php
Last active December 20, 2015 02:59 — forked from sterlingwes/tourney.html
對戰表的繪製
<!DOCTYPE html>
<html>
<head>
<title>Tournament Bracket Generator</title>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
$(document).on('ready', function() {
var knownBrackets = [2,4,8,16,32], // brackets with "perfect" proportions (full fields, no byes)
@WenLiangTseng
WenLiangTseng / back_end_ajax.php
Created July 23, 2013 07:44
Wordpress AJAX 基本寫法格式
<?php
function example_ajax_request() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {
$fruit = $_REQUEST['fruit'];
// Let's take the data that was sent and do something with it
if ( $fruit == 'Banana' ) {