2012年6月6日 星期三

文字處理

文字處理:

strpos

--- 找出字串第一次出現的位置
語法 : int strpos (string haystack, string needle [, int offset])

substr



substr --- 取得部份字串

語法 : string substr (string string, int start [, int length])

explode ---將字串依指定的字串或字元separator切開

語法:array explode(string separator, string string,[int limit])

將字串 "宅配通:123456789" 套用explode,條件":"
$a=explode(':',"宅配通:123456789");
$a[0]='宅配通',$a[1]='123456789'

CODE:

例子:
$ac=exam_560
$pos= strpos($ac,'_');
$id =substr($ac,0,$pos);
$exam_name= substr($ac,$pos+1);
針對字串第一個字是否為大寫!!ex:身分證 Q123584123

CODE:

$b=substr($a,0,1);取得第一個字

//研判第一個字元是不是在A(ASCII值是65)到Z(95)之間,若是,那第一個字母就是大寫字元。
if ( ord($b) >= 65 AND ord($b) <= 95 ) {
echo  "驗證合格!";
}  else {
echo  "第一個字元不是大寫字母!";
} 
補充:ord a: 97 z:127 A: 65 Z: 95

str_replace

mixed str_Replace(mixed search, mixed replace,mixed subject , int &count)
$str=str_replace("mrsoft","吉林省明日科技有限公司","Welcome to mrsoft!");

結果:Welcome to 吉林省明日科技有限公司

例子:將雙引號取代成 code

str_replace('\"', "&quot;",$content);

取得目前頁面的檔案名稱

echo basename(__FILE__);

取得網址(URL中的檔案含副檔名)

例如網址是:http://127.0.0.1/myTest/MyHttp.php
則要利用什麼方法,可以擷取出此文字:MyHttp.php

參考解:
$arr = explode('/',$_SERVER['PHP_SELF']);
$filename = $arr[count($arr)-1];

#當前運行腳本所在的文檔根目錄。在服務器配置文件中定義。

$_SERVER['DOCUMENT_ROOT'];

去掉最後一個逗號/去尾巴

substr($str,0,strrpos($str,','))

亂數

$a=rand(最小值[整數],最大值[整數])

在CKEDITOR 中丟入從word復製的文字或表格,有太多的不必要的word的字元,我想要把那些字完踢掉:
找到一個東西叫preg_replace() 正規表示式取代字串
微軟字元都藏在 span 中,所以把 span全部抓掉

摘錄其中我用的一些功能,詳細功能,請上網找。

$body = preg_replace("/\/",'',$publications2); 
濾掉 

沒有留言: