2014年11月27日 星期四

PHP fgetcsv, str_getcsv 中文問題

在 OpenShift 上使用 str_getcsv 時中文會有問題,檔案爲 UTF-8 編碼,locale 爲 zh_TW.UTF-8

PHP 4 使用 fgetcsv
PHP 5 使用 str_getcsv

確定可用代碼 :
 function __fgetcsv(& $handle, $length = null, $d = ‘,’, $e = ‘”‘) {
     $d = preg_quote($d);
     $e = preg_quote($e);
     $_line = “”;
     $eof=false;
     while ($eof != true) {
         $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
         $itemcnt = preg_match_all(’/’ . $e . ‘/’, $_line, $dummy);
         if ($itemcnt % 2 == 0)
             $eof = true;
     }
     $_csv_line = preg_replace(’/(?: |[ ])?$/’, $d, trim($_line));
     $_csv_pattern = ‘/(’ . $e . ‘[^' . $e . ']*(?:’ . $e . $e . ‘[^' . $e . ']*)*’ . $e . ‘|[^' . $d . ']*)’ . $d . ‘/’;
     preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
     $_csv_data = $_csv_matches[1];
     for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
         $_csv_data[$_csv_i] = preg_replace(’/^’ . $e . ‘(.*)’ . $e . ‘$/s’, ‘$1′, $_csv_data[$_csv_i]);
         $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
     }
     return empty ($_line) ? false : $_csv_data;
 }




References :
linux下,fgetcsv乱码问题 - Alian的专栏 - 博客频道 - CSDN.NET
萬隆小站 • 檢視主題 - [php]fgetcsv 讀入檔案無「「」時,第一字元被刪掉亂碼
[php]UTF-8的fgetcsv函數 @ 清新下午茶 :: 痞客邦 PIXNET ::

2014年11月24日 星期一

CodeIgniter api route

http://localhost:81/index.php/api/user/index/1
http://localhost:81/index.php/api/user/1
http://localhost:81/index.php/api/user

Route::any('api/(:any)/(:any)/(:num)', '$1/api/$2/$3');
Route::any('api/(:any)/(:num)', '$1/api/index/$2');
Route::any('api/(:any)/(:any)', '$1/api/$2');
Route::any('api/(:any)', '$1/api');

public function index($id = FALSE)
{  
    $return_data = array('user_id' => $id, 'status' => 'success', message' => '');
                                                                                                                                   
    echo json_encode($return_data);
}




References :
Parsing URI segments and assigning them to variables - Bonfire Forums
Docs - My Bonfire

JavaScript pass arguments in setInterval function

setInterval( function() { funca(10,3); }, 500 );




References :
javascript - Pass parameters in setInterval function - Stack Overflow

2014年11月19日 星期三

Bootstrap lightbox(Modal) example



References :
JavaScript · Bootstrap

PHP if words in string

function contains($needle, $haystack)
{
    return strpos($haystack, $needle) !== false;
}
 
if(contains('bb', 'aa bb cc'))
{ 
    echo 'true';
}




References :
php - Check if string contains specific words? - Stack Overflow

CodeIgniter file upload example



References :
File Uploading Class : CodeIgniter User Guide

CodeIgniter get all column in table

$this->db->list_fields('table');




References :
php - How to get field names of mysql table in codeigniter? - Stack Overflow

CodeIgniter get post value array

 name="id[]" value="1"/>
 name="id[]" value="2"/>
 name="id[]" value="3"/>
$id = $this->input->post('id');




References :
php - How can I get post data into an array in CodeIgniter? post items are arrays - Stack Overflow

2014年11月18日 星期二

PHP count file lines

$file = "/tmp/somefile.txt"; 
 
$lines = count(file($file)); 
 
echo "There are $lines lines in $file";




References :
Count the number of lines in a text file - Totally PHP

PHP read file line by line

$file = fopen("/tmp/test.txt","r");

while(!feof($file))
{
    echo fgets($file). "
";
}

fclose($file);




References :
PHP fgets() Function

2014年11月16日 星期日

CodeIgniter get insert id

$this->xxx_model->insert($data);
$this->db->insert_id();




References :
php - CodeIgniter activerecord, retrieve last insert id? - Stack Overflow

CodeIgniter SESSION

$this->load->library("session");  // if you not load
 
$this->session->set_userdata("SESSION_NAME","VALUE");
echo $this->session->userdata("SESSION_NAME");  




References :
php - How to save and extract session data in codeigniter - Stack Overflow

2014年11月13日 星期四

Install PHP GD image library

PHP Fatal error: Call to undefined function imagefontwidth() 
PHP Fatal error: Call to undefined function imagecreatefrompng()
 
 
Debian / Ubuntu :
# apt-get install php5-gd
# /etc/init.d/apache2 restart  
CentOS : 
# yum install php-gd




References :
php - Install GD library and freetype on Linux - Stack Overflow

2014年11月11日 星期二

PySNMP bulkCmd example

getbulk-v2c.py





References :
PySNMP Examples: High-level API to SNMP Standard Applications: Command Generator

Ubuntu install snmpd

# apt-get install snmp snmpd

# vi /etc/snmp/snmpd.conf
#  Listen for connections from the local system only
#agentAddress  udp:127.0.0.1:161
#  Listen for connections on all interfaces (both IPv4 *and* IPv6)
agentAddress udp:161,udp6:[::1]:161
# /etc/init.d/snmpd restart




References :
How To Install and Configure an SNMP Daemon and Client on Ubuntu 14.04 | DigitalOcean

 

Docker run image bind UDP port

$ docker run -p 161:161/udp snmpd




References :
Dnamically bind both tcp and udp on a same port · Issue #4635 · docker/docker

Docker failed to load system roots and no roots provided

Get https://index.docker.io/v1/repositories/ubuntu/images: x509: failed to load system roots and no roots provided

# apt-get install ca-certificates
# /etc/init.d/docker restart




References :
/ccmp/tech: 5月 2014

Git https verify error

fatal: unable to access 'https://github.com/pozgo/docker-snmpd.git/': Problem with the SSL CA cert (path? access rights?)

$ export GIT_SSL_NO_VERIFY=true
$ git clone https://github.com/pozgo/docker-snmpd.git/




References :
git - SSL certificate rejected trying to access GitHub over HTTPS behind firewall - Stack Overflow

SQLAlchemy query where not null

seats = Seat.query.filter(Seat.invite != None).all()
 
 
 
 
References : 
ython - flask sqlalchemy querying a column with not equals - Stack Overflow

2014年11月10日 星期一

Bonfire default username and password


Your Email: admin@mybonfire.com
Password: password

Bonfire database migration

  1. Enter Web, Database / Database Tools / Migrations
    http://localhost/index.php/admin/developer/migrations
  2.  auto migrates
$ vi application/config/config.php
$config['migrate.auto_core']  = TRUE;
$config['migrate.auto_app']   = FALSE;




References :
Docs - My Bonfire

Bonfire Unable to locate proper database settings. Please verify settings and reload the page.

$ cp application/config/database.php application/config/development/database.php




References :
Docs - My Bonfire

Install phpmd (PHP Mess Detector)

# apt-get install phpmd

Install phpcs (PHP CodeSniffer)

# pear install PHP_CodeSniffer




References :
squizlabs/PHP_CodeSniffer

2014年11月8日 星期六

2014年11月7日 星期五

Yii CDbConnection failed to open the DB connection.

Yii 預設使用 SQLite Database,PHP 需要安裝函式庫

PHP Fatal error:  Uncaught exception 'PDOException' with message 'could not find driver' in /var/www/demos/blog/protected/data/dbgen.php:7 

# apt-get install sqlite php5-sqlites
# /etc/init.d/apache2 restart




2014年11月5日 星期三

add PPA GPG key

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9D1A0061



References :
[SOLVED] how to add the PPA key

2014年11月3日 星期一

command line send message to IRC

#!/bin/bash

IRC_SERVER='irc.freenode.net'
IRC_PORT=6667
IRC_USERNAME='h4'
IRC_FULLNAME='h4'
IRC_NICK='h4'                                                                                                             
IRC_CHANNEL='#h4'

send_irc_message() {
    message=$1

    echo -e "USER $IRC_USERNAME 8 * : $IRC_FULLNAME\nNICK $IRC_NICK\nJOIN $IRC_CHANNEL\nPRIVMSG :$IRC_CHANNEL $1\nQUIT\n" | nc $IRC_SERVER $IRC_PORT
}

send_irc_message test





References :
ubuntu - Send message to IRC channel from bash - Server Fault
Nunca — Sending messages to an IRC server from a shell script (Bash)

訊息空格問題
sockets - IRC BOT (python) - Stack Overflow

command line convert date string to timestamp

$ date --date='06/12/2012 07:21:22' +"%s"




References :
shell - Convert date time string to UNIX timestamp in bash command - Stack Overflow