Categories
Programming/php

[PHP] php 由 pipe (stdin) 讀進檔案 再做處理, 類似 C 的 pipe line 方法

$cnt = 0;
$fp = fopen('php://stdin','r');
while ($line = stream_get_line($fp, 65535, "\n")) {
  echo "LINE #" . (++$cnt) . ": " . $line . "\n";
}

Categories
Programming/php System/Linux/Unix*

PECL PHP REDIS client library phpredis

URL : https://github.com/phpredis/phpredis#classes-and-methods

redis.ini file in /etc/php5/conf.d with the following contents: extension=redis.so

Categories
DATABASE Programming/php

[PHP] Sample code : Berkeley DB , bdb , dba_open , db4 , key-value database ,

$id = dba_open("bdb4_tsid.db", "w", "db4");

if (!$id) { echo "dba_open failed\n"; exit; }

dba_replace("key", "This is an example!", $id);

if (dba_exists("key", $id)) {
        echo dba_fetch("key", $id);
        dba_delete("key", $id);
}

dba_close($id);

Screenshot 2015 05 13 20 28 39

 

 

Screenshot 2015 05 13 20 37 56

 

This is an example!
exist:
This is an example!
---> This is an example!

Categories
Programming/php

PHP Regex Validation of Hexadecimal Values / check hex number

preg_match( '/^[A-F0-9]{15,18}/',$v)

 

Screenshot 2015 05 10 10 53 23

Categories
Programming/php

PHP 的 mktime , time format , 週編號 , week number of year, weeks starting on Monday

Screenshot 2015 02 06 15 31 50

 

1419696000
52 - 2014-12-29 00:00:00
52 - 2014-12-30 00:00:00
52 - 2014-12-31 00:00:00
00 - 2015-01-01 00:00:00
00 - 2015-01-02 00:00:00
00 - 2015-01-03 00:00:00
00 - 2015-01-04 00:00:00
01 - 2015-01-05 00:00:00
01 - 2015-01-06 00:00:00
01 - 2015-01-07 00:00:00

Categories
Programming/php

[php] array_key_exists vs isset 那一個比較快?

這些微差異要在大量的 loop 中才會有一些差別, 不過還是寫 code 時一次寫好吧 —> 給我自己的提醒!

答案是 isset 比較快!

ref. URL: http://www.alternateinterior.com/2006/11/comparing-array_key_exists-with-isset.html

相關資訊: A HOWTO on Optimizing PHP
http://phplens.com/lens/php-book/optimizing-debugging-php.php

Tuning Apache and PHP for Speed on Unix
http://phplens.com/phpeverywhere/tuning-apache-php

Categories
Programming/php

[php] regular expression match

preg_match( '/c=([0-9]+)/' , $t->log['dest_query'] , $data);
這樣可以取出 c=xxxx

抓 user agent 版本

preg_match( '/MSIE ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_msie_ver);
preg_match( '/Windows NT ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_nt_ver);
  if ( !( (float)$data_msie_ver[1]>=6 && (float)$data_msie_ver[1]<=8 ) ) $valid_useragent = 0;
Categories
Copy_N_Paste DATABASE Programming/php

USE Oracle / oci8 with PHP

前提環境要先裝好 Oracle instant client , 然後 PHP 要把 instantclient 編譯進去

–with-oci8=instantclient,/usr/lib/oracle/10.2.0.3/client/lib

這是 sample code

$conn = OCILogon('scott', 'scott', '//172.30.0.176/DB1');

$query = 'select * from dept';

$stid = OCIParse($conn, $query);
OCIExecute($stid, OCI_DEFAULT);
while ($succ = OCIFetchInto($stid, $row)) {
  foreach ($row as $item) {
    echo $item." ";
  }
  echo "\n";
}

OCILogoff($conn);

心得是
1. 建 oracle table 時不管key 的 table name , field name 是否為小寫, 在 oracle 都顯示成大寫
2. 用 PHP 的 OCI functions 下的 SQL command 也可以不管大小寫
3.

Categories
Programming/javascript Programming/php

範例程式 jQuery /JSON / PHP /json_encode / json_decode sample code

jQuery : 請參考這篇 : http://jsgears.com/thread-63-1-1.html

Microsoft 也提供 video 教學: http://msdn.microsoft.com/zh-tw/asp.net/dd310332.aspx#jQuery

$(document).ready( function(){
  var data = new Object();
  data.hello = "Hello";
  data.world = 'World';
  data.worked = " it worked ";
  data.somebool = true;
  data.array = new Array("he\"ll\"o", '"World"');
  var dataString = $.toJSON(data);
  $.post('phpfile.php', {data: dataString}, function(res){
      var obj = $.evalJSON(res);
      if(obj.somebool === true)
      $("#result").html(obj.hello + ' ' + obj.array[1] + obj.worked + ". Message from PHP: "+obj.php_message);
    });
});

… 未完

Categories
Programming/php

PHP 小筆記

[code language=’php’]

[/code]

這個常數在 CLI mode 下是 cli
在 apache 下跑的話是 apache2handler