2009年9月30日 星期三

ALSA 測試

一步一步看聲音出不來的問題
Alsa-sound-mini-HOWTO, 6. Testing and using
很好的ALSA筆記
Linux ALSA sound notes

2009年9月25日 星期五

PHP的utf-8斷句

PHP的substr遇到UTF-8字元,沒切到邊界時.會出現亂碼。所以就要判斷UTF-8有幾個字節。

function moss_truncate($str, $cut) {
$len = strlen($str);
$count = 0; // count
$i = 0; // cut index

if($len < $cut)
return $str;

do {
if($count >= $cut)
return substr($str, 0, $i) . '...';

$value = ord($str[$i]); // ASCII value
if($value > 191 and $value < 224) // 2 bytes
$i+=2;
elseif($value > 223 and $value < 240) // 3 byte
$i+=3;
elseif($value > 239 and $value < 248) // 4 bytes
$i+=3;
else // others, include ASCII (less than 128)
$i++;

$count++;
} while($i < $len);
return $str;
}

$str = '蘑d,プリプリで美味';
echo moss_truncate($str, 8); //取8個"字", 不管是ASCII還是Unicode都算一個字
// 結果:
// 蘑d,プリプリで

參考:
解決中文字串的斷字問題?
PHP如何判斷是否為utf8編碼文件的方法

2009年9月23日 星期三

HTML的input直接按Enter就送出表單

如果有加<form></form>,應該就會自動submit出去,但是如果不要的話,就要在<input>裡加onKeyDown的JavaScript事件,13就是按enter的key code。下面範例不是送出表單(文不對題!!),而是用剛輸入的資料當參數,重導到另一個網址。

<input name="q" type="text" onKeyDown="javascript:(function(){if (event.keyCode ==13) document.location.href='foo?q='+document.getElementById('bar').value })()">

Javascript的Key code參考
Javascript Tips - Event Key Codes
或是可以線上直接按按看碼的是多少
Javascript Key Event Test Script

2009年9月22日 星期二

PacketVideo - OpenCore - OSCL (Android)

在電腦的角落撿到,我已經忘了這是為了什麼而寫的...

+-----------------+
| PVActiveBase | oscl/oscl/osclproc/src/oscl_scheduler_ao.cpp
+-----------------+
^
|
+-----------------+
| OsclActiveObject| oscl/oscl/osclproc/src/oscl_scheduler_ao.cpp
+-----------------+
^
|
+-----------------+
| OmxComponentBase| codecs_v2/omx/omx_baseclass/src/pv_omxcomponent.cpp
+-----------------+
^
|
+-----------------+
|OmxComponentVideo| codecs_v2/omx/omx_baseclass/src/pv_omxcomponent.cpp
+-----------------+
^
|
+-----------------+
| OpenmaxAvcAO | codecs_v2/omx/omx_h264/src/omx_avc_component.cpp
+-----------------+



oscl_scheduler_aobase.h
class PVActiveBase
/**
* PV Scheduler internal AO base class. Both OsclActiveObject
* and OsclTimerObject derive from this class. For Symbian, this
* just container has the desired additions to the basic CTimer or OsclActiveObj
* functionality.
* For non-Symbian, this class contains the entire AO implementation.
*/


oscl_scheduler_ao.h
class OsclActiveObject:
/**
* User base class for execution objects.
* OsclActiveObject defines an execution object without any timer.
* This AO can be used across threads, i.e. the request
* can be activated in one thread and completed in another.
*/

omx_avc_component.cpp
AvcOmxComponentFactory
// This function is called by OMX_GetHandle and it creates an instance of the avc component AO

2009年9月21日 星期一

Emacs寫HTML

C-c C-t 加tag, 然後照指示加attribute: property, value.
C-c C-v 開browser看
C-c C-f 移到下個匹配tag
C-c C-b 移到上個匹配tag
C-c Tab 隱藏HTML tag, 再打一次C-c Tab就顯示回來
C-c / 加入結尾tag
C-c C-a 在HTML tag裡加attribute
C-c C-d 刪除HTML tag開頭和結尾都會一起刪
C-c RET 插入<p>
C-c j 插入<br>
C-c C-n 插入特殊字元, 像空白&nbsp;, 小於&lt;, 大於&gt;...
C-c C-c h 插入<a href=...>
C-c C-c u 插入<ul><li>...</ul>
C-c C-c o 插入<ol><li>...</ol>
C-c C-c c 插入checkbox
C-c C-c r 插入radio

C-c C-h 看說明

好用的PHP RSS parser: SimplePie

多年前增用過"喜鵲"Magpie RSS Parser,現在看到網頁長的跟以前都一樣,好像沒什麼更新。找了一下果然有更好用的SimplePie。也沒時間比較,至少感覺更簡單好用。

步驟如下:
1) 下載
2) 建立名為"cache"的目錄
3) 引入simpiepie.inc (其實也只需要這個檔案)

require_once 'simplepie.inc';

4) 建立一個SimplePie物件, 就可以去資料結構裡撈了!

$feed = new SimplePie('http://feeds2.feedburner.com/moogoo');
$feed->handle_content_type(); // This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->get_permalink(); // feed's link
$feed->get_title(); // feed's title
$feed->get_description(); // feed's description

foreach ($feed->get_items() as $item) {
$item->get_permalink(); // item's link
$item->get_title(); // item's title
$item->get_description();
$item->get_date('j F Y | g:i a');
}

2009年9月18日 星期五

用Emacs編dot (graphviz)

用Emacs來寫dot (Graphviz)也很方便.有人寫了Graphviz dot mode for emacs

1) 到以上網站下載graphviz-dot-mode.el
2) 在.emacs加(load-file "PATH_TO_FILE/graphviz-dot-mode.el")
3) 指令:
M-x compile或C-c c -> 編譯dot, 其實是幫你執行"dot -Tpng foo.dot > foo.png", 但是我都用設定好的F5, 因為C-c c常會按成C-x c就離開了, 囧rz
C-c p -> 顯示png

2009年9月17日 星期四

MySQL的算資料總數COUNT/SUM

要從資料庫裡抓統計的數目出來,除了用基本的COUNT外,SUM也相當好用,因為可以在SUM裡加像IF, CASE等條件式。
SELECT SUM(CASE WHEN status='xxx' THEN 1 END) FROM atable
SELECT SUM(status='xxx' AND name='yyy') FROM atable

之前都要分成好幾次query出來,真是笨!

2009年9月11日 星期五

GRUB 2和GRUB Legacy的命令列比較

Commands

Linux kernel VGA table


Resolution in pixels
Color depth | 640x480 800x600 1024x768 1280x1024
256 (8bit)| 769 771 773 775
32000 (15bit)| 784 787 790 793
65000 (16bit)| 785 788 791 794
16.7 Mill.(24bit)| 786 789 792 795


via: http://en.opensuse.org/SDB:Setting_up_Unsupported_Graphics_Cards_with_the_Framebuffer_Device_(GRUB)
Linux video mode numbers (Wikipedia)

Intel 800/900 Series VBIOS Hack : version 0.5.2

Chipset: 915GM
BIOS: TYPE 1
Mode Table Offset: $C0000 + $269
Mode Table Entries: 36

Mode 30 : 640x480, 8 bits/pixel
Mode 32 : 800x600, 8 bits/pixel
Mode 34 : 1024x768, 8 bits/pixel
Mode 38 : 1280x1024, 8 bits/pixel
Mode 3a : 1600x1200, 8 bits/pixel
Mode 3c : 800x480, 24 bits/pixel
Mode 41 : 640x480, 16 bits/pixel
Mode 43 : 800x600, 16 bits/pixel
Mode 45 : 1024x768, 16 bits/pixel
Mode 49 : 1280x1024, 16 bits/pixel
Mode 4b : 1600x1200, 16 bits/pixel
Mode 4d : 800x480, 16 bits/pixel
Mode 50 : 640x480, 32 bits/pixel
Mode 52 : 800x600, 32 bits/pixel
Mode 54 : 1024x768, 32 bits/pixel
Mode 58 : 1280x1024, 32 bits/pixel
Mode 5a : 1600x1200, 32 bits/pixel
Mode 5c : 800x480, 32 bits/pixel

2009年9月7日 星期一

[PHP] 亂數怎樣才夠亂

原本嫌PHP的亂數產生函數rand()不好用,就算用了說明書上有寫更好的mt_rand()也是一樣,不能按照時間每次執行都產生夠亂的數。所以自己用microtime()當參數來產生每次都不一樣的亂數字串,後來發現有手冊有更簡單的方式:

$better_token = md5(uniqid(mt_rand(), true));
via: PHP: uniqid

如果要對密碼做hash,可參考這篇PHP Security Consortium: Password Hashing

編輯器的編碼::Editor's Coding

用Windows的筆記本(Notepad)打om二字, 然後按Enter, 再打個, 存成不同的編碼來看:

ANSI:
6F 6D 0D 0A D4 7B
UTF-8:
EF BB BF 6F 6D 0D 0A E5 94 B5
Unicode(預設是little-endian):
FF FE 6F 00 6D 00 0D 00 0A 00 35 55

* 0D 0A是ASCII的CR LF, Windows用的換行, Unix則用LF, Mac用CR. 所以windows編過的檔案到Linux上常會看到^M的標誌. [LF(Line Feed) 就是換行, CR(Carriage Return)是因為以前用電報機, 換行後還是回到第一格.]
* Unicode編碼開頭的U+FEFF是BOM(byte-order mark), UTF-8的BOM則是EF BB BF 
* 唵的Unocode

Emacs:
C-h C ENTER (看現在的coding)
M-x list-coding-systems (列出全部coding system)
C-x ENTER f coding ENTER (改用coding system)
C-x ENTER c coding ENTER (用coding system來看目前的buffer)

Vim:
" 設定自動轉換為 UTF-8 編碼
set fileencodings=utf-8,big5,euc-jp,gbk,euc-kr,utf-bom,iso8859-1 "在讀寫檔案的編碼
set encoding=utf8
set tenc=utf8
set ff=unix

如果PHP讀到有BOM格式的檔案, 會當成一般字元輸出, 如果用到header()要導向別頁時,就會出現:

Cannot modify header information - headers already sent...的錯誤訊息

2009年9月2日 星期三

[jQuery] 表格最後增加一列

真是方便好用~

$('#addRow').click(function(){
$('#myTable tbody>tr:last').clone(true).insertAfter('#myTable tbody>tr:last');
});