php strtok函数,strtok函数的使用示例
strtok函數是字符串函數庫中的一個函數,函數原型如下:
char *strtok(char s[], const char *delim);
作用:分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
例如:"hello,hi:what?is!the.matter;" 把這串字符串傳入strtok函數,第二個delim寫 ",:?!.;" , 這樣就可以得到6個不同的子字符串。
我們來寫個例子驗證一下,就寫分割時間的例子吧,獲取UTC時間
如下:
#include
#include
#include
int main()
{
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
struct tm *p;
char buf[100] = {0};
char *q ;
time_t timep;
time(&timep); /*獲得time_t結構的時間,UTC時間*/
p = gmtime(&timep); /*轉換為struct tm結構的UTC時間*/
sprintf(buf,"%d/%d/%d-%s-%d:%d:%d\n",
1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday,
wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
printf("%s\n",buf);
q = strtok(buf,"//--::");
printf("q : %s\n",buf);
while(1)
{
q = strtok(NULL ,"//--::");
if(q == NULL)
break ;
printf("q : %s\n",q);
}
return 0;
}
運行結果:
2017/8/17-Thu-8:24:43
q : 2017
q : 8
q : 17
q : Thu
q : 8
q : 24
q : 43
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
總結
以上是生活随笔為你收集整理的php strtok函数,strtok函数的使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序流式布局
- 下一篇: JS中如何阻止事件的传播