Skip to content

Instantly share code, notes, and snippets.

@WenLiangTseng
WenLiangTseng / wp_auto_p.php
Created July 23, 2013 07:06
wpautop(WordPress Functions),讓文字自動加上 HTML 的 <p> 標籤
<?php
function wpautop($pee, $br = true) {
$pre_tags = array();
if ( trim($pee) === '' )
return '';
$pee = $pee . "\n"; // just to make things a little easier, pad the end
if ( strpos($pee, '<pre') !== false ) {
@WenLiangTseng
WenLiangTseng / convert_timestamp_to_php_date.php
Created July 22, 2013 13:13
PHP 將資料庫的 timestamp 轉換成其他的日期格式(如年/月/日)
<?php
//$timestamp = $row->date; // get your timestamp data from MySQL database's table field.
$timestamp = "2008-10-05 21:34:02";
echo date("Y/n/d", strtotime( $timestamp ));
//other examples:
echo date("F j, Y g:i a", strtotime( $timestamp )); // October 5, 2008 9:34 pm
echo date("m.d.y", strtotime( $timestamp )); // 10.05.08
echo date("j, n, Y", strtotime( $timestamp )); // 5, 10, 2008
echo date("Ymd", strtotime( $timestamp )); // 20081005
@WenLiangTseng
WenLiangTseng / php_id_number_checker.php
Created July 22, 2013 06:21
PHP身分證檢查函數
<?php
// 身份證檢查函數
function check_identity($id) {
$flag = false;
$id = strtoupper($id);
$d0 = strlen($id);
$qd = "";
if ($d0 <= 0) {$qd=$qd."還沒填呢 !n";}
@WenLiangTseng
WenLiangTseng / how_to_make_wp_image_chinese_path_to_validate_w3c.php
Created July 22, 2013 06:19
讓WP中文圖片網址符合W3C檢驗(讓get_the_post_thumbnail()符合W3C檢驗的方法)
<?php
//讓Wordpress的中文圖片網址符合W3C檢驗的方法
//讓get_the_post_thumbnail()符合W3C檢驗的方法
//也就是說,讓中文的網址經過一層URL編碼
$thumbnail_id = get_post_thumbnail_id($post->ID);
$src = wp_get_attachment_image_src( $thumbnail_id );
@WenLiangTseng
WenLiangTseng / php_how_many_days_between_two_dates.php
Created July 22, 2013 06:17
PHP計算兩個日期之間的天數
<?php
//----- 計算兩個日期之間的天數 -----//
$startTimeStamp = strtotime("2011/07/01");
$endTimeStamp = strtotime("2011/07/17");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
@WenLiangTseng
WenLiangTseng / add_class_to_a_parent_of_a_submenu.php
Created July 22, 2013 06:17
給Wordpress的次選單之parent加上class,將擁有submenu的li,添加一個指定的class進去
<?php
/*
* This adds a "dropdown" class to menus
*/
function oikos_add_menu_parent_classes( $classes, $item, $args ) {
$children = get_posts( array(
'meta_query' => array (
array(
'key' => '_menu_item_menu_item_parent',
'value' => $item->ID )
@WenLiangTseng
WenLiangTseng / remove_wordpress_auto_added_p_tags.php
Created July 22, 2013 06:16
移除Wordpress自己加上去的p tag
<?php
function simonbattersby_shortcode_format($content){
$content = preg_replace('/(<p>)\s*(<div)/','<div',$content);
$content = preg_replace('/(<\/div>)\s*(<\/p>)/', '</div>', $content);
return $content;
}
add_filter('the_content','simonbattersby_shortcode_format',11);
@WenLiangTseng
WenLiangTseng / php_get_google_map_info_by_http_request.php
Created July 22, 2013 06:15
PHP用HTTP request取得Google Map資訊的方法
<?php
$url = 'http://maps.google.com/maps/geo?q=EC3M,+UK&output=csv&sensor=false';
$data = @file_get_contents($url);
$result = explode(",", $data);
echo $result[0]; // status code
echo $result[1]; // accuracy
@WenLiangTseng
WenLiangTseng / generate_order_number.php
Created July 22, 2013 06:14
PHP產生一個訂單編號(獨一無二的訂單編號)
<?php
$stamp = strtotime("now");
$orderid = $stamp - $_SERVER['REMOTE_ADDR'];
$orderid = str_replace(".", "", $orderid);
?>
@WenLiangTseng
WenLiangTseng / sorting_a_2d_array.php
Created July 22, 2013 06:11
PHP 排序一個二維陣列的方式,將一個二維陣列依據裡面的 key 來排序
<?php
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter); //由低至高排序
//arsort($sorter); 由高至低排序