Archive

Archive for the ‘PHP’ Category

แก้ Function Character Limiter ใน Codeigniter ให้รองรับภาษาไทย

May 20, 2014 Leave a comment
if ( ! function_exists('character_limiter'))
{
    function character_limiter($str, $n = 500, $end_char = '…')
      {
           if (mb_strlen($str, "UTF-8") < $n)
           {
               return $str;
           }
 
           $str = preg_replace("/\s+/u", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
 
           if (mb_strlen($str, "UTF-8") <= $n)
           {
               return $str;
           }
 
           $out = "";
           foreach (explode(' ', trim($str)) as $val)
           {
               $out .= $val.' ';
 
               if (mb_strlen($out, "UTF-8") >= $n)
               {
                   $out = trim($out);
                   return (mb_strlen($out, "UTF-8") == mb_strlen($str, "UTF-8")) ? $out : $out.$end_char;
               }
           }
     }
}

Code: http://pastebin.com/AzWwswpt

Instagram Subscription Tag

June 4, 2013 Leave a comment

1. ยิง Url ไปสมัครด้วยวิธีง่าย เข้าลิ้งก์นี้ http://instagram.com/developer/api-console/

2. Auth Console ให้เรียบร้อย เลือก Request เป็น Subscriotion Tag

http://www.google.com = Url ที่จะให้ข้อมูลยิงเข้ามา

object = ประเภท;

object_id = ชื่อ Tag

[Body]

aspect=media;callback_url=http://www.google.com;object=tag;object_id=tpk;

[verify_token] = Access Token

[client_id] = Id App ที่ใช้ยิง

[client_secret] = Secret Key

Categories: PHP

PHP function to javascript

June 26, 2012 Leave a comment

php function ที่แปลงไปเป็น ภาษา js
http://phpjs.org/

Categories: Jquery, PHP Tags: ,

Get next MySQL Auto Increment

May 12, 2012 Leave a comment

function nextAi($table_name) {
$r = mysql_query(“SHOW TABLE STATUS LIKE ‘$table_name'”);
$row = mysql_fetch_array($r);
$auto_increment = $row[‘Auto_increment’];
mysql_free_result($r);
return $auto_increment;
}

วิธีนี้จะให้ผลลัพธ์ที่ตรงกว่าการใช้ Max (id) + 1 เพราะสมมุติว่าสูงสุดคือไอดี 40 และโดนลบไป Max (id) + 1 ก็จะเท่ากับ 40 เหมือนเดิม ซึ่งจริง ๆ แล้ว Table รันไปถึงเลข 41 แล้ว การใช้วิธีนี้จะเป็นการดึง AI จาก Table ที่ต้องการโดยตรง

Categories: PHP Tags: , ,

Facebook Batch Upload API

May 2, 2012 Leave a comment
 
$files = array(
    '/data/Pictures/pic1.jpg',
    '/data/Pictures/pic2.jpg',
    '/data/Pictures/pic3.jpg'
);

//Must set upload support to true
$facebook->setFileUploadSupport(true);

$batch     = array();
$params    = array();
$count = 1;
foreach($files as $file){
    $req = array(
        'method'         => 'POST',
        'relative_url'   => '/me/photos',
        'attached_files' => 'file' . $count
    );
    //add this request to the batch ...
    $batch[] = json_encode($req);

    //set the filepath for the file to be uploaded
    $params['file'.$count] = '@' . realpath($file);

    $count++;
}
$params['batch'] = '[' . implode(',',$batch) . ']';

try{
    $upload = $facebook->api('/','post',$params);
} catch(FacebookApiException $e) {
    error_log($e);
    $upload = null;
}

//View the results ...
if(!is_null($upload)){
    echo "<pre>" . print_r($upload,1) . "<pre>";
    echo "<hr />";
}

Categories: PHP Tags: , , ,

ฟังก์ชั่นเช็ค GIF ว่าเป็น Animation หรือไม่

February 16, 2012 Leave a comment

function is_ani($filename) {
$filecontents = file_get_contents($filename);

$str_loc = 0;
$count = 0;
while ($count < 2) { # There is no point in continuing after we find a 2nd frame
$where1 = strpos($filecontents, “\x00\x21\xF9\x04”, $str_loc);
if ($where1 === FALSE) {
break;
} else {
$str_loc = $where1 + 1;
$where2 = strpos($filecontents, “\x00\x2C”, $str_loc);
if ($where2 === FALSE) {
break;
} else {
if ($where1 + 8 == $where2) {
$count++;
}
$str_loc = $where2 + 1;
}
}
}

if ($count > 1) {
return(true);
} else {
return(false);
}
}

Categories: PHP Tags: , , ,

แก้ SSI ของ SMF ให้แสดงผลภาษาไทยไม่เพี้ยน

February 6, 2012 Leave a comment

เหตุเกิดจากตอนที่พยายามจะใช้ SSI ของ SMF ดึงรายการ Recent Topic แต่ปัญหาก็เกิด

คือภาษาไทยแสดงผลไม่ถูกต้อง เป็นเครื่องหมาย ????? ส่วนภาษาอังกฤษแสดงได้ปกติ

ทั้ง ๆ ที่ meta header ก็เป็น UTF8 อยู่แล้ว Database เช็คหมดแล้วก็เป็น UTF General CI

แล้วปัญหามาจากไหนล่ะ !!! สืบไปสืบมาได้เรื่องว่าเกิดจากตัว SSI นี่แหละ

วิธีแก้

1. เปิดไฟล์ SSI.php ขึ้นมา

2. ค้นหาข้อความ loadDatabase();

3. ใส่ mysql_query(‘set names utf8’, $db_connection); ลงในบรรทัดข้างล่าง ต่อจากข้อ 2

หมายเหตุ

1. ที่แก้เป็น SMF Version 2

2. เช็ค Header เว็บคุณว่าเป็น UTF8 แล้วหรือยัง

3. เช็ค Database ที่ prefix smf ว่าเป็น UTF8 แล้วหรือยัง (ถ้ายังต้องเปลี่ยนแล้วล้างข้อมูล SMF)

 

Convert Relative Path to Absolute Path

January 13, 2012 Leave a comment

function reltoabs($host, $path) {

$resulting = array();
$hostparts = parse_url($host);
$pathparts = parse_url($path);
if (array_key_exists(“host”, $pathparts)) return $path; // Absolute
// Relative
$opath = “”;
if (array_key_exists(“scheme”, $hostparts)) $opath .= $hostparts[“scheme”] . “://”;
if (array_key_exists(“user”, $hostparts)) {
if (array_key_exists(“pass”, $hostparts)) $opath .= $hostparts[“user”] . “:” . $hostparts[“pass”] . “@”;
else $opath .= $hostparts[“user”] . “@”;
} elseif (array_key_exists(“pass”, $hostparts)) $opath .= “:” . $hostparts[“pass”] . “@”;
if (array_key_exists(“host”, $hostparts)) $opath .= $hostparts[“host”];
if (!array_key_exists(“path”, $pathparts) || $pathparts[“path”][0] != “/”) {
$dirname = explode(“/”, $hostparts[“path”]);
$opath .= implode(“/”, array_slice($dirname, 0, count($dirname) – 1)) . “/” . basename($pathparts[“path”]);
} else $opath .= $pathparts[“path”];
if (array_key_exists(“query”, $pathparts)) $opath .= “?” . $pathparts[“query”];
if (array_key_exists(“fragment”, $pathparts)) $opath .= “#” . $pathparts[“fragment”];
return $opath;

}

Categories: PHP Tags: ,

Fire Log Spark – Codeigniter log viewer

January 13, 2012 Leave a comment

Download/Screenshot :: Fire Log Spark

วิธีติดตั้งโดยไม่ใช้ Spark

1. ดาวน์โหลดและนำไฟล์ทุกไฟล์ไปวางตามตำแหน่ง Folder ยกเว้นไฟล์ config/autoload.php

2. สร้าง Class ใหม่ขึ้นมาในโฟลเดอร์ controlelr ยกตัวอย่างเช่น log.php (Class log)

3. ใน __construct ให้ทำการสั่งโหลดไฟล์ของ Fire Log Spark ตัวอย่าง Class ด้านล่าง

<?php

if (!defined(‘BASEPATH’))exit(‘No direct script access allowed’);

Class Log extends CI_Controller {

function __construct() {

parent::__construct();
$this->load->helper(‘spark_url’);
$this->load->helper(‘fire_log’);
$this->load->config(‘fire_log’);
$this->load->language(‘fire_log’);
$this->load->library(‘fire_log’);

}

function index(){

}

}

?>

4. ตรวจสอบ config/config.php ตรงค่า $config[‘log_threshold’] ปรับเอาตามต้องการ ถ้าค่าตรงนี้เท่ากับ 0 จะไม่มีการเขียน Log ครับ

5. เช็คโฟลเดอร์ application/logs ว่า chmod 777 ไว้หรือไม่ ถ้ายัง chmod 777 ด้วย

6. หลังจากนั้นให้เข้า URL ของ Class และ Function index ตัวอย่างเช่น http://www.domain.com/index.php/log/index

*** ตรง function index สร้างไว้เปล่าๆ ตามตัวอย่างก็พอครับ เข้าตามลิ้งค์ตัวอย่างด้านบน library Fire Log จะทำงานของมันเองจ้าาาา

How to header CSV to UTF-8

October 26, 2011 Leave a comment
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=Customers_Export.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
Categories: PHP Tags: , ,