Category Archives: Chỉ dẫn lập trình

Copy dữ liệu text là tiếng Việt vào clipboard

/*Language: C++.
IDE: Dev-cpp*/
int GetSize(UChar* message) {
    int i = 0, r = 0;
    while(message[i] != 0) {
        i++;
    }
    r = i * 2;
    return r;
}
BOOL SetClipboard(UChar* msg) {
    int size = GetSize(msg);
    if (!OpenClipboard(NULL)) return FALSE;
    EmptyClipboard();
    HGLOBAL hClipboardData;
    hClipboardData = GlobalAlloc(GMEM_DDESHARE, size + 1);
    UChar* x = (UChar*) GlobalLock(hClipboardData);
    memset(x, 0, size);
    memcpy(x, msg, size);
    GlobalUnlock(hClipboardData);
    SetClipboardData(CF_UNICODETEXT, hClipboardData);
    CloseClipboard();
    return TRUE;
}

UTF-8 for PHP and MySQL

I can’t insert correctly characters encoded in utf-8 in my database (I use phpmyadmin to deal with the data in there)
The thing is when I check the content of my tables, I get weird characters for foreign characters. For example, when I send a japanese character with a form from my own website, I get 症治疗取 in phpmyadmin

The page that contains my form has a utf-8 charset and my table collation is utf8_bin.
The other thing is that when I send my request directly in phpmyadmin, all the characters are well inserted and everything is right.

So I guess I must be missing some function to deal with the data I send with my form so that it will be well UTF 8 encoded but I don’t see what I should use

Adding mysql_query(“SET NAMES ‘UTF8′”); solved above problem

Lấy nhạc beat trên star.zing.vn

Input: http://star.zing.vn/star/phong-thu/Cau-Vong-Khuyet.5339.html
Output: http://static.star.zing.vn/karaokeoriginal/mp3/vietnam/Cau_vong_khuyet_Tuan_Hung_admin1215501232.jpg

Dạng chuẩn input: http://star.zing.vn/star/phong-thu/xyz.N.html
Lấy N, ghép thành http://star.zing.vn/includes/fnGetSongInfo.php?id=N
Ứng với ví dụ mẫu là 5339, http://star.zing.vn/includes/fnGetSongInfo.php?id=5339
Tùy ngôn ngữ lập trình, phân tích mã nguồn trang này ta thu được link output.
Người dùng download link output và đổi đuôi tệp tin thành mp3.

Demo download nhạc beat từ star.zing.vn http://huypv.net/blog/star.asp

Tự động resize ảnh trên trang web

function addOnloadEvent(fnc) {
    if (
typeof window.addEventListener != “undefined”)
        
window.addEventListener(“load”, fnc, false);
    else if (
typeof window.attachEvent != “undefined”) {
        
window.attachEvent(“onload”, fnc);
    }
    else {
        if (
window.onload != null) {
            var 
oldOnload = window.onload;
            
window.onload = function(e) {
                
oldOnload(e);
                
window[fnc]();
            };
        }
        else
            
window.onload = fnc;
    }
}
function 
changeSize() {
    
MAX_WIDTH=400;
    var 
imgs = document.getElementsByTagName(“img”);
    var 
l = imgs.length;
    for (
i = 0; i < l; i++) {
        var 
img = imgs.item(i);
        
cw = img.width;
        if (
cw > MAX_WIDTH) {    
            
img.width = MAX_WIDTH;
            
img.height = img.height * MAX_WIDTH / cw;
      &
nbsp; }
    }
}
addOnloadEvent(changeSize);