Skip to content

Instantly share code, notes, and snippets.

@WenLiangTseng
WenLiangTseng / wordpress_chinese_excerpt.php
Created July 22, 2013 05:50
Wordpress-中文摘要寫法
<?php
//Normal Version
function chinese_excerpt($text, $lenth=100) {
$text = mb_substr($text,0, $lenth);
return $text;
}
add_filter('the_excerpt', 'chinese_excerpt');
//Improved Version
//加上UTF-8參數以及strip_tags,
@WenLiangTseng
WenLiangTseng / wordpress_sort_search_result.php
Created July 22, 2013 05:48
Wordpress 排序-搜尋結果排序,將搜尋結果依照關聯程度來排序
<?php
//------- 搜尋結果依照關聯性排序 --------//
if(isset($_GET['s'])){
add_filter('posts_orderby_request', 'search_orderby_filter');
}
function search_orderby_filter($orderby = ''){
global $wpdb;
$keyword = $wpdb->prepare($_REQUEST['s']);
return "((CASE WHEN {$wpdb->posts}.post_title LIKE '%{$keyword}%' THEN 10 ELSE 0 END) + (CASE WHEN {$wpdb->posts}.post_content LIKE '%{$keyword}%' THEN 1 ELSE 0 END)) DESC, {$wpdb->posts}.post_date DESC";
@WenLiangTseng
WenLiangTseng / php_deal_cards.php
Created July 22, 2013 05:47
PHP發牌程式碼,可以發牌給兩個玩家
<?php
//$player1 = array();
//$player2 = array();
$card_array = array(
'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13',
'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10', 'H11', 'H12', 'H13',
'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11', 'D12', 'D13',
'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C13',
@WenLiangTseng
WenLiangTseng / php_use_html_mail_template.php
Created July 22, 2013 05:46
PHP使用HTML郵件版型
<?php
/*You could put a PHP script inside the template
and then have PHP itself do the rendering,
for example:
*/
//template.html:
?>
<div clas="headr"></div>
@WenLiangTseng
WenLiangTseng / curl_example.php
Created July 22, 2013 05:41
使用CURL抓資料的PHP函數,可以給予URL資料,然後得到回傳的結果
<?php
$xml_file = file_get_contents(SITE_PATH . 'cms/data.php');
//CURL
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
@WenLiangTseng
WenLiangTseng / codeigniter_back_to_previous_page_function.php
Created July 22, 2013 05:41
在CodeIgniter中,建立回到前一頁的功能
<?php
//
function __construct()
{
parent::__construct();
[...]
if (isset($_SERVER['HTTP_REFERER']))
@WenLiangTseng
WenLiangTseng / codeigniter_upload_image_and_text_controller.php
Created July 22, 2013 05:39
CodeIgniter中,同時上傳圖片與文字
<?php
// controller
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
@WenLiangTseng
WenLiangTseng / codeigniter_call_this_in_function.php
Created July 22, 2013 05:35
CodeIgniter-在函數裡呼叫$this的方法
<?php
function is_logged_in() {
// Get current CodeIgniter instance
$CI =& get_instance();
// We need to use $CI->session instead of $this->session
$user = $CI->session->userdata('user_data');
if (!isset($user)) { return false; } else { return true; }
}
@WenLiangTseng
WenLiangTseng / codeigniter_mysql_date_judgement.php
Created July 22, 2013 05:34
CodeIgniter 使用日期判斷的 MySQL 語法
<?php
// if you still want to use AR,
// you could pass a FALSE as third parameter of
// $this->db->where
// to avoid automatic escaping by CI:
$this->db->select('id,name')
->where('dr','1')
->where('end >=', 'CURDATE()', FALSE);
$query = $this->db->get('store');
@WenLiangTseng
WenLiangTseng / php_mail_function.php
Created July 22, 2013 05:20
PHP寄送郵件的寫法
<?php
/*使用PHP mail()寄送UTF-8編碼之電子郵件
常遇到的問題是,「郵件標題」或「寄件者」是亂碼的問題
主要的原因在於,電子郵件標準格式中,
表頭的部分不允許使用雙位元的文字,
所以,使用mb_encode_mimeheader()函式
將雙位元文字編碼為單位元字串。
*/