1. 安裝 atomic repository
wget -q -O - http://www.atomicorp.com/installers/atomic | sh
安裝完畢會看到 /etc/yum.repos.d/atomic.repo
2. 升級 php 即完成
yum php update
Ref: https://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/
2013年3月11日 星期一
2012年12月23日 星期日
[PHP] Highlight search result
一般我們在做搜尋時,輸出的結果都會對 keyword 做 highlight 來凸顯比對結果
像字串 How to highlight search result 如果以 highlight, result 為 keyword 來搜尋
我們一般期待輸出的結果為 How to <span class=highlight>highlight</span> search <span class=highlight>result</span>
如果單純以 str_replace 用迴圈來對每個 keyword 比對時,在跑第二次也會發生問題
或是用 array 來建立 keys 和 replace 也會有點麻煩
一個簡單的寫法是使用 preg_replace 來處理
以下是範例
<style>
.highlight {color:red}
</style>
<?php
$keys[] = 'highlight';
$keys[] = 'result';
$text = 'How to highlight search result.';
echo findKeywords_org($text, $keys) . '<br>';
echo findKeywords($text, $keys) . '<br>';
function findKeywords_org($text, $keys)
{
$replace = array();
foreach($keys as $k)
{
$replace[] = "<span class=highlight>$k</span>";
}
return str_ireplace($keys, $replace, $text);
//這是 str_replace 不分大小寫的版本,但限定 php5 以上才有,故在 php4 要不分大寫法也須要自行處理,會很麻煩
}
function findKeywords($text, $keys)
{
$text = htmlspecialchars($text);
$patterns = '/('. implode('|', $keys) .')/i'; //i 是不分大小寫
$replacements = '<span class=highlight>${0}</span>'; //${0}所以比對 patterns 到的字串 value
return preg_replace($patterns, $replacements, $text);
}
Ref: http://php.net/manual/en/function.preg-replace.php
2012年12月22日 星期六
[JavaScript] Variable dump
一個簡單的方法是
另一個是寫個 dump 函數
Example:
console.log(object);
另一個是寫個 dump 函數
function dump(obj) {
var out = '';
for (var i in obj) {
if (typeof obj[i] === "object")
out += i + ": {\n" + dump(obj[i]) + "}\n";
else
out += i + ": " + obj[i] + "\n";
}
return out;
}
Example:
<script>
var book = {
name: 'Javascript tutorial',
price: 50,
index: {
1: 'introduction',
2: 'hello world'
},
toString: function () { return "name: " + this.name + ",price: " + this.price; }
}
function dump(obj) {
var out = '';
for (var i in obj) {
if (typeof obj[i] === "object")
out += i + ": {\n" + dump(obj[i]) + "}\n";
else
out += i + ": " + obj[i] + "\n";
}
return out;
}
console.log(book);
console.log(dump(book));
</script>
[PHP] PHP array to Javascript json object
<?php
$books = array();
$books[] = array('name' => 'php tutorial',
'author' => 'php',
'price' => 100
);
$books[] = array('name' => 'javascript tutorial',
'author' => 'javascript',
'price'=> 50
);
?>
<script>
var books = <? echo json_encode($books) ?>;
for(var i=0; i<books.length; i++)
{
console.log('name: ' + books[i].name);
console.log('author: ' + books[i].author);
console.log('price: ' + books[i].price);
}
</script>
2012年12月4日 星期二
[CSS] CSS 選擇器
筆記起來...
ref: http://www.w3.org/TR/CSS2/selector.html
ref: http://www.w3.org/TR/CSS2/selector.html
| Pattern | Meaning | Described in section |
|---|---|---|
| * | Matches any element. | Universal selector |
| E | Matches any E element (i.e., an element of type E). | Type selectors |
| E F | Matches any F element that is a descendant of an E element. | Descendant selectors |
| E > F | Matches any F element that is a child of an element E. | Child selectors |
| E:first-child | Matches element E when E is the first child of its parent. | The :first-child pseudo-class |
| E:link E:visited | Matches element E if E is the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited). | The link pseudo-classes |
| E:active E:hover E:focus | Matches E during certain user actions. | The dynamic pseudo-classes |
| E:lang(c) | Matches element of type E if it is in (human) language c (the document language specifies how language is determined). | The :lang() pseudo-class |
| E + F | Matches any F element immediately preceded by a sibling element E. | Adjacent selectors |
| E[foo] | Matches any E element with the "foo" attribute set (whatever the value). | Attribute selectors |
| E[foo="warning"] | Matches any E element whose "foo" attribute value is exactly equal to "warning". | Attribute selectors |
| E[foo~="warning"] | Matches any E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "warning". | Attribute selectors |
| E[lang|="en"] | Matches any E element whose "lang" attribute has a hyphen-separated list of values beginning (from the left) with "en". | Attribute selectors |
| DIV.warning | Language specific. (In HTML, the same as DIV[class~="warning"].) | Class selectors |
| E#myid | Matches any E element with ID equal to "myid". | ID selectors |
[PHP] PHP 型態比較表
筆記起來...
ref: http://php.net/manual/en/function.empty.php
ref: http://php.net/manual/en/function.empty.php
| Expression | gettype() | empty() | is_null() | isset() | boolean : if($x) |
|---|---|---|---|---|---|
| $x = ""; | string | TRUE | FALSE | TRUE | FALSE |
| $x = null | NULL | TRUE | TRUE | FALSE | FALSE |
| var $x; | NULL | TRUE | TRUE | FALSE | FALSE |
| $x is undefined | NULL | TRUE | TRUE | FALSE | FALSE |
| $x = array(); | array | TRUE | FALSE | TRUE | FALSE |
| $x = false; | boolean | TRUE | FALSE | TRUE | FALSE |
| $x = true; | boolean | FALSE | FALSE | TRUE | TRUE |
| $x = 1; | integer | FALSE | FALSE | TRUE | TRUE |
| $x = 42; | integer | FALSE | FALSE | TRUE | TRUE |
| $x = 0; | integer | TRUE | FALSE | TRUE | FALSE |
| $x = -1; | integer | FALSE | FALSE | TRUE | TRUE |
| $x = "1"; | string | FALSE | FALSE | TRUE | TRUE |
| $x = "0"; | string | TRUE | FALSE | TRUE | FALSE |
| $x = "-1"; | string | FALSE | FALSE | TRUE | TRUE |
| $x = "php"; | string | FALSE | FALSE | TRUE | TRUE |
| $x = "true"; | string | FALSE | FALSE | TRUE | TRUE |
| $x = "false"; | string | FALSE | FALSE | TRUE | TRUE |
| TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | "" | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TRUE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE |
| FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE |
| 1 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 0 | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE |
| -1 | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
| "1" | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| "0" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
| "-1" | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
| NULL | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | TRUE |
| array() | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE |
| "php" | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE |
| "" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE |
| TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | "" | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TRUE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 1 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| 0 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| -1 | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| "1" | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
| "0" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
| "-1" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
| NULL | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
| array() | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE |
| "php" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE |
| "" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |
2012年11月27日 星期二
[伺服器管理] inotify + rsync 完成即時同步資料
inotify 是個強大的檔案監控系統
可以監控所指定目錄下所有檔案變化,包含新增,刪除,修改...等事件
Linux kernel 需要 2.6.13 以才能有支援
故可以利用其機制 + rsync 在發現檔案系統有變化時即執行 rsync 來做同步
rsync 是個好用且強大的同步備份軟體,但可惜的是他在同步時通常是全目錄一起比較後 Diff 把 Diff 部份傳輸,如果檔案很多時,將會消耗許多系統資源。
所以利用 inotily + rsync 來降低其消耗資源,也可以達到即時同步
以下範例將實現將一台 Master 的網頁資料同步至另一台 Slave Server
rsync 遠端同步
將 master 資料(/var/www/myweb) 同步至 slave (/var/www/myweb)
ssh 192.168.1.2
rsync -avz root@192.168.1.1:/var/www/myweb /var/www/
(會要求輸入密碼,如不想輸入密碼請建立 ssh-key 並在指令加上 -e "ssh -i [key-path]")
ps一般情況不需即時同步,即可以使用 crontab job 來做定時同止
1 3 * * * /usr/bin/rsync -avz -e "ssh -i /root/.ssh/priv-key" root@192.168.1.1:/var/www/myweb /var/www/
上述的 rsync 已經是使用 ssh 登入再做 rsync 可以不用架 server,如果需求上有需要 rsync server 可以參考最後一段。
再來安裝 inotify來做即時監控並同步資料
Master:
1. 安裝 inotify
a. 請至 https://github.com/rvoicilas/inotify-tools/wiki/ 下載 inotify-tools
b. 將 inotify-tools-3.14.tar.gz 上傳至 master
c. tar -zxvf inotify-tools-3.14.tar.gz
d. cd inotify-tools-3.14
e. ./configure
f. make
g. make install
2. 使用以下指令測試監控目錄 (/var/www/myweb)
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f %e' --event modify,create,move,delete /var/www/myweb
(可以使用另個 ssh 登入並在該目錄下新增刪除檔案)
完整 event 可參考
http://linux.die.net/man/7/inotify
http://en.wikipedia.org/wiki/Inotify
3. 建立 script 用於啟動監控並即時同止
a. vi inotify-tools.sh
加入以下內容
4. 開機時自動啟動
vi /etc/rc.d/rc.local
在後面加入
/root/inotify-tools.sh &
#請依據您放 inotify-tools.sh 的目錄修改
ps.如果不使用 rsync 亦可以使用 nfs + mount 的方式來處理
僅需將 cmd="rsync -avz $src root@$host:$dest"此行的指令
最後 root@$host:$dest 修改為 mount Slave上來目錄路徑即可
(option) 利用 rsync server 來同步資源
Slave: (登入 Slave)
1. yum install rsync xinetd
2. vi /etc/xinetd.d/rsync
內容
service rsync
{
disable = no #改為 no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
#並加入以下內容
[backup] #表示在使用rsync指令時目錄代號 (與 samba 設定相似)
path = /var/www #表示實際指向的目錄
uid = root
gid = root
hosts allow = 192.168.1.0/24 #充許的存取的 host,請依 ip 需求修改
read only = no
3. service xinetd restart
Master:
此時即可將 master 資料(/var/www/myweb) 同步至 slave (/var/www/myweb)
rsync -arq --delete /var/www/myweb 192.168.1.2::backup/
ps一般情況不需即時同步,即可以使用 crontab job 來做定時同止
1 3 * * * /usr/bin/rsync -arq --delete /var/www/myweb 192.168.1.2::backup/
可以監控所指定目錄下所有檔案變化,包含新增,刪除,修改...等事件
Linux kernel 需要 2.6.13 以才能有支援
故可以利用其機制 + rsync 在發現檔案系統有變化時即執行 rsync 來做同步
rsync 是個好用且強大的同步備份軟體,但可惜的是他在同步時通常是全目錄一起比較後 Diff 把 Diff 部份傳輸,如果檔案很多時,將會消耗許多系統資源。
所以利用 inotily + rsync 來降低其消耗資源,也可以達到即時同步
以下範例將實現將一台 Master 的網頁資料同步至另一台 Slave Server
rsync 遠端同步
將 master 資料(/var/www/myweb) 同步至 slave (/var/www/myweb)
ssh 192.168.1.2
rsync -avz root@192.168.1.1:/var/www/myweb /var/www/
(會要求輸入密碼,如不想輸入密碼請建立 ssh-key 並在指令加上 -e "ssh -i [key-path]")
ps一般情況不需即時同步,即可以使用 crontab job 來做定時同止
1 3 * * * /usr/bin/rsync -avz -e "ssh -i /root/.ssh/priv-key" root@192.168.1.1:/var/www/myweb /var/www/
上述的 rsync 已經是使用 ssh 登入再做 rsync 可以不用架 server,如果需求上有需要 rsync server 可以參考最後一段。
再來安裝 inotify來做即時監控並同步資料
Master:
1. 安裝 inotify
a. 請至 https://github.com/rvoicilas/inotify-tools/wiki/ 下載 inotify-tools
b. 將 inotify-tools-3.14.tar.gz 上傳至 master
c. tar -zxvf inotify-tools-3.14.tar.gz
d. cd inotify-tools-3.14
e. ./configure
f. make
g. make install
2. 使用以下指令測試監控目錄 (/var/www/myweb)
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f %e' --event modify,create,move,delete /var/www/myweb
(可以使用另個 ssh 登入並在該目錄下新增刪除檔案)
完整 event 可參考
http://linux.die.net/man/7/inotify
http://en.wikipedia.org/wiki/Inotify
3. 建立 script 用於啟動監控並即時同止
a. vi inotify-tools.sh
加入以下內容
#!/bin/sh
src='/var/www/myweb'
dest='/var/www'
host='192.168.1.2'
/usr/local/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f %e' \
--event modify,create,move,delete $src | while read date time file event
do
# echo $date $time $file $event
# cmd="rsync -arq --delete $src $host::backup$dest" old command
cmd="rsync -avz $src root@$host:$dest"
# echo $cmd
$cmd
done
4. 開機時自動啟動
vi /etc/rc.d/rc.local
在後面加入
/root/inotify-tools.sh &
#請依據您放 inotify-tools.sh 的目錄修改
ps.如果不使用 rsync 亦可以使用 nfs + mount 的方式來處理
僅需將 cmd="rsync -avz $src root@$host:$dest"此行的指令
最後 root@$host:$dest 修改為 mount Slave上來目錄路徑即可
(option) 利用 rsync server 來同步資源
Slave: (登入 Slave)
1. yum install rsync xinetd
2. vi /etc/xinetd.d/rsync
內容
service rsync
{
disable = no #改為 no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
#並加入以下內容
[backup] #表示在使用rsync指令時目錄代號 (與 samba 設定相似)
path = /var/www #表示實際指向的目錄
uid = root
gid = root
hosts allow = 192.168.1.0/24 #充許的存取的 host,請依 ip 需求修改
read only = no
3. service xinetd restart
Master:
此時即可將 master 資料(/var/www/myweb) 同步至 slave (/var/www/myweb)
rsync -arq --delete /var/www/myweb 192.168.1.2::backup/
ps一般情況不需即時同步,即可以使用 crontab job 來做定時同止
1 3 * * * /usr/bin/rsync -arq --delete /var/www/myweb 192.168.1.2::backup/
訂閱:
文章 (Atom)
