PHP 正規表達式,清除隱藏字元,整齊排列輸出的字元

清除隱藏字元,整齊排列輸出的字元,
其實這並不是一個很重要的工作啦,
但如果你看到下面輸出的XML會感到很痛苦的,
那你跟我一樣有這種偏執狂

看到這樣亂七八糟真的很痛苦,
主要是因為我們在編輯程式的時候,
有些隱藏字元會被我們加在字串中,
當輸出的時候就會一起被輸出,
比較長見的隱藏字元 n r t 就是造成這些很怪縮排的兇手,
我們可以透過正規表達式把這些字元取代,
第二個參數空白就是刪除,
echo preg_replace(‘/[nrt]/’, ‘ ‘, $xml);

不過這樣好像太乾淨了齁,
修改一下不要拿掉斷行n,

echo preg_replace(‘/[rt]/’, ‘ ‘, $xml);

哇屋~是不是舒服多了。

[……]

閱讀更多

PHP simplexml to Array XML 轉 Array

引用網址
https://gist.github.com/tfnet/2037443

PHP 使用 simplexml_load_string() 解析 XML 資料,
要一直 $node->children() 一個節點一個節點的取,這樣用實在很麻煩,
如果能一次把 XML 結構資料轉成 Array 在讀裡面的資料就非常好用了,
網路查了一下看到神人分享的function,完全用原本的XML結構打造成Array,非常好用,
該網址提供的程式碼較適用於Class中,我做了一點小小的修改,
原版本請連文章上方引用網址,修改後版本請於文章下方 Download

<?php
/**
 * Converts a simpleXML element into an array. Preserves attributes.<br/>
 * You can choose to get your elements either flattened, or stored in a custom
 * index that you define.<br/>
 * For example, for a given element
 * <code>
 * <field name=”someName” type=”someType”/>
 * </code>
 * <br>
 * if you choose to flatten attributes, you would get:
 * <code>
 * $array[‘field’][‘name’] = ‘someName’;
 * $array[‘field’][‘type’] = ‘someType’;
 * </code>
 * If you choose not to flatten, you get:
 * <code>
 * $array[‘field’][‘@attributes’][‘name’] = ‘someName’;
 * </code>
 * <br>__________________________________________________________<br>
 * Repeating fields are stored in indexed arrays. so for a markup such as:
 * <code>
 * <parent>
 *     <child>a</child>
 *     <child>b</child>
 *     <child>c</child>
 * …
 * </code>
 * you array would be:
 * <code>
 * $array[‘parent’][‘child’][0] = ‘a’;
 * $array[‘parent’][‘child’][1] = ‘b’;
 * …And so on.
 * </code>
 * @param simpleXMLElement    $xml            the XML to convert
 * @param boolean|string    $attributesKey    if you pass TRUE, all values will be
 *                                            stored under an ‘@attributes’ index.
 *                                            Note that you can also pass a string
 *                                            to change the default index.<br/>
 *                                            defaults to null.
 * @param boolean|string    $childrenKey    if you pass TRUE, all values will be
 *                                            stored under an ‘@children’ index.
 *                                            Note that you can also pass a string
 *                                            to change the default index.<br/>
 *                                            defaults to null.
 * @param boolean|string    $valueKey        if you pass TRUE, all values will be
 *                                            stored under an ‘@values’ index. Note
 *                                            that you can also pass a string to
 *                                            change the default index.<br/>
 *                                            defaults to null.
 *
 * @return array the resulting array.
 */
function simpleXMLToArray($xml,$attributesKey=NULL,$childrenKey=NULL,$valueKey=NULL)
{

if($childrenKey && !is_string($childrenKey)){
$childrenKey = ‘@children’;
}
if($attributesKey && !is_string($attributesKey)){
$attributesKey = ‘@attributes’;
}
if($valueKey && !is_string($valueKey)){
$valueKey = ‘@values’;
}

$return = array();
$name = $xml->getName();
$_value = trim((string)$xml);
if(!strlen($_value)){
$_value = null;
};

if($_value!==null){
if($valueKey){
$return[$valueKey] = $_value;
}
else{$return = $_value;
}
}

$children = array();
$first = true;
foreach($xml->children() as $elementName =&g[……]

閱讀更多

PHP POST RAW 使用 cURL

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            “http://yourdomain.com.tw” );

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_HTTPHEADER,     array(‘Content-Type: text/plain’)); 
curl_setopt($ch, CURLOPT_POSTFIELDS,     “POST RAW 資料” ); 


$result=curl_exec ($ch);


引用資料

RAW POST using cURL in PHP
http://stackoverflow.com/questions/871431/raw-post-using-curl-in-php

[……]

閱讀更多

PHP big5轉utf8不要用iconv(),iconv這個函數,用mb_convert_encoding

不要以為big5轉utf8就用iconv()這麼簡單

big5有很多字是沒有收錄的:

測試:

用法:

原文出自:

http://sweslo17.blogspot.tw/2012/04/big5-erpms-sql-local-cache-phpiconv.htmlhttp://dev.sopili.net/2009/08/phpbig5utf8iconv.htmlhttp://help.i2yes.com/?q=node/65

[……]

閱讀更多

PHP 數字字串取奇數或偶數, 奇數/偶數如何判斷

奇數及偶數判斷我想應該很多方法
我這邊提供一種方法判斷
就是數值除2取餘數的方式

//偶數餘0
2/2 餘 0
4/2 餘 0
6/2 餘 0

//奇數餘1
1/2 餘 1
3/2 餘 1
5/2 餘 1

運算子

在PHP裡取餘數的運算子為 %
echo 6%2 ;  //結果 0

應用

//數字字串轉數字陣列
$str_arr = str_split(‘14063070084’);

//取奇數
foreach($str_arr as $k=>$v){
if(!($k%2))echo $v;
}

//取偶數
foreach($str_arr as $k=>$v){
if($k%2)echo $v;
}[……]

閱讀更多

php .htaccess 實用範例

關閉 magic_quotes_gpc

# Only if you use PHP
<ifmodule mod_php4.c>
php_flag magic_quotes_gpc off
</ifmodule>

減少 Bandwidth 使用量

# Only if you use PHP
<ifmodule mod_php4.c>
php_value zlib.output_compression 16386
</ifmodule>

變更 scripts 副檔名

AddType application/x-httpd-php .php4

以上語句會將 .php4 當成 PHP 程式編譯。

禁止 .htaccess 開放瀏覽

<files file-name>
order allow,deny
deny from all
</files>

變更預設頁面

DirectoryIndex myhome.htm index.htm index.php

自訂錯誤頁面

ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

開放/禁止瀏覽目錄

# 禁止
Options All -Indexes
# 開啟
Options +Indexes

本文出自
http://www.hkcode.com/linux-bsd-notes/438

Apache Rewrite with Htaccess 理解與技巧

[……]

閱讀更多

PHP array union 陣列聯集

//array 聯集

array_unique(array_merge($array1, $array2, $array3));

//合併陣列
array_merge(); 
如果鍵名有重複,該鍵的鍵值為最後一個鍵名對應的值(後面的覆蓋前面的)。如果數組是數字索引的,則鍵名會以連續方式重新索引。
註釋:如果僅僅向array_merge() 函數輸入了一個數組,且鍵名是整數,則該函數將返回帶有整數鍵名的新數組,其鍵名以0 開始進行重新索引。

//清除重複的值
array_unique(); 
函數移除數組中的重複的值,並返回結果陣列。
當幾個數組元素的值相等時,只保留第一個元素,其他的元素被刪除。
返回的數組中鍵名不變。



參考
http://www.w3school.com.cn/php/func_array_unique.asp
http://www.w3school.com.cn/php/func_array_merge.asp

[……]

閱讀更多

MySQL 常用資料型態

MySQL 資料庫的資料欄位型態可分為文字類型、數值類型、日期時間類型,與特殊類型,下面列出常用的欄位型態:
文字型態
適用於儲存文字資料的欄位型態,其中 VARCHAR 、 CHAR 必須指定文字長度 實際儲存的資料超過設定的文字長度,雖可儲存但超過的部份將被截斷。例如儲存文字「 How are you ? 」到文字長度設定為 10 的欄位,將只剩下「 How are yo 」,若設定過度的文字長度則會佔用儲存空間,建議視實際儲存的資料需求設定文字型態。
資料型態儲存位元說明VARCHAR (M)

1~255

非 固 定 長 度 字 元 的 資 料 型 態
例 如 儲 存 通 訊 地 址 。
CHAR (M)

1~255

固 定 長 度 字 元 的 資 料 型 態
例 如 儲 存 身 份 證 字 號 。
TINYTEXT

255

適 用 於 儲 存 255 字 元 以 內 的 資 料TEXT

65535

適 用 於 儲 存 較 多 字 元 的 資 料
例 如 訪 客 留 言 板 的 內 容
MEDIUMTEXT

1677215

適 用 於 儲 存 大 容 量 文 字 的 資 料LONGTEXT

4294967295

適 用 於 儲 存 超 大 容 量 文 字 的 資 料
數值型態
適於於儲存數字資料的欄位型態,例如會員編號、商品價格、學生成績等,設定為 signed 時資料範圍可以是負值,設定為 unsigned 時資料範圍僅能儲存正值。必須注意數值型態的儲存範圍,例如商品價格若為 5000 元,儲存於 TINYINT 數值型態的欄位,查詢時將變成 127 元 ( signed ) 或 255 元 ( unsigned ),造成顯示或計算錯誤的問題,若設定較大的資料範圍則會佔用儲存空間,同樣建議視實際儲存的數值上限設定數值型態。
資料型態位元資料範圍TINYINT

1

signed: -128 ~ 127
unsigned: 0 ~ 255
SMALLINT

2

signed: -32768 ~ 32767
unsigned: 0 ~ 65535
MEDIUMINT

3

signed: -8388608 ~ 8388607
unsigned: 0 ~ 16777215
INT

4

signed: -2147483648 ~ 2147483647
unsigned: 0 ~ 4294967295
BIGINT

8

signed: -9223372036854775808 ~ 9223372036854775807
unsigned: 0 ~ 1844674407370951615
日期時間型態
資料型態儲存位元資料範圍DATE3 bytes1000-01-01 ~ 9999-12-31TIME3 bytes-838:59:59 ~ 838:59:59DATETIME8 bytes1000-01-01 00:00:00 ~ 9999-12-31 23:59:59

有關 MySQL 資料型態的詳細介紹可參考官方網站的說明:
http://dev.mysql.com/doc/refman/5.0/en/data-types.html
文章出處
http://www.cg.com.tw/php/htm/Dreamweaver_005.asp

[……]

閱讀更多

PHP substr 取得某一區段文字 字串分割

<?php

$rest = substr(“abcdef”, 0 , 1); // returns “a”
$rest = substr(“abcdef”, 2 , 2); // returns “cd”
$rest = substr(“abcdef”, -1); // returns “f”
$rest = substr(“abcdef”, -2); // returns “ef”
$rest = substr(“abcdef”, -3, 1); // returns “d”

$rest = substr(“abcdef”, 0, -1);  // returns “abcde”
$rest = substr(“abcdef”, 2, -1);  // returns “cde”
$rest = substr(“abcdef”, 4, -4);  // returns false
$rest = substr(“abcdef”, -3, -1); // returns “de”
?>

原文

http://us2.php.net/manual/en/function.substr.php[……]

閱讀更多