天天看點

php經典執行個體

1.周遊字元串

<?php

string="Thisweekend,I′mgoingshoppingforapetchicken.";string="Thisweekend,I′mgoingshoppingforapetchicken.";vowels = 0;

for(i=0,i=0,j = strlen(string);string);i<j;j;i++){

    if(strstr('aeiouAEIOU',string[string[i])){

        vowels++;     } } echovowels++;     } } echovowels;

?>

輸出結果:14。

周遊字元串,獲得其中所有的元音字母個數,這裡的strstr(para1,para2)方法可以判斷參數para2是否在para1字元串中,如果在則傳回true,此時$vowels變量加1。當周遊完字元串後就可以獲得最終的元音字幕個數了。時間複雜度為O(n)。

2.讀取檔案資訊

fp=fopen(′hello.txt′,′r′)ordie("can′topenfile");while(fp=fopen(′hello.txt′,′r′)ordie("can′topenfile");while(s = fgets(fp,1024))$fields[1]=substr($s,0,10);$fields[2]=substr($s,10,5);$fields[3]=substr($s,15,12);printr($fields);fclose(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方法。

本文轉自TBHacker部落格園部落格,原文連結:http://www.cnblogs.com/jiqing9006/archive/2012/07/03/2574508.html,如需轉載請自行聯系原作者