1.遍歷字串
<?phpst
ring
="thi
swee
kend
,i′m
goin
gsho
ppin
gfor
apet
chic
ken.
";string="thisweekend,i′mgoingshoppingforapetchicken.";
vowels = 0;
for(i=0
,i=0,
j = strlen(str
ing)
;string);
ii++) } echo
vowels++; } } echo
vowels;
?>
輸出結果:14。
遍歷字串,獲得其中所有的母音字母個數,這裡的strstr(para1,para2)方法可以判斷引數para2是否在para1字串中,如果在則返回true,此時$vowels變數加1。當遍歷完字串後就可以獲得最終的母音字幕個數了。時間複雜度為o(n)。
2.讀取檔案資訊
<?phpfp
=fop
en(′
hell
o.tx
t′,′
r′)o
rdie
("ca
n′to
penf
ile"
);wh
ile(
fp=fopen(′hello.txt′,′r′)ordie("can′topenfile");while(
s = fgets(fp,
1024))
$fie
lds[
1]=s
ubst
r($s
,0,10
);$f
ield
s[2]
=sub
str(
$s,10
,5);
$fie
lds[
3]=s
ubst
r($s
,15,12
);pr
intr
($fi
elds
);fc
lose
(fp,1024))$fields[1]=substr($s,0,10);$fields[2]=substr($s,10,5);$fields[3]=substr($s,15,12);printr($fields);fclose(
fp) or die("can't close file");
?>
hello.txt內容為:
hello, my name is jack.
hello, my name is tom.
hello, my name is jim.
輸出結果:array ( [1] => hello, my [2] => name [3] => is jack. ) array ( [1] => hello, my [2] => name [3] => is tom. ) array ( [1] => hello, my [2] => name [3] => is jim. )
首先開啟檔案hello.txt,然後一行一行地獲取檔案中的資訊。將資訊切分成三段儲存到陣列變數fields中。輸出資訊。
處理完成後,關閉檔案。這裡分別用到了fopen方法,fgets方法和fclose方法。
php遞迴演算法經典例項
遞迴函式為自呼叫函式,在函式體內直接或間接自己呼叫自己,但需要設定自呼叫的條件,若滿足條件,則呼叫函式本身,若不滿足則終止本函式的自呼叫,然後把目前流程的主控權交回給上一層函式來執行,可能這樣給大家講解,還是很難明白,直接上例子 function test n else echo n.test 2 ...
python經典例項 python經典例項
本書是python經典例項解析,採用基於例項的方法編寫,每個例項都會解決具體的問題和難題。主要內容有 數字 字串和元組,語句與語法,函式定義,列表 集 字典,使用者輸入和輸出等內建資料結構,類和物件,函式式和反應式程式設計,web服務,等等。python是傑出的指令碼語言,可以為應用程式注入動力,提...
PHP經典例項讀書筆記 變數
empty 會告訴你乙個值是否為空。isset 會告訴你乙個值是否設定。空的定義 integer 0 double 0.0 string 空串 string 0 這個要注意字串 0 也為空 boolean false 陣列 array null 物件 empty 在5.5以前只允許傳入引數,5.5以...