Hướng dẫn dùng c strtok_r trong PHP

Tadius

11-07-2010, 09:05 PM

Rõ ràng là chưa hiểu hàm strtok
Cậu có hiểu ý nghĩa của NULL trong lời gọi hàm không? Nó chỉ định là lấy token kế tiếp (trong chuỗi đang xét, tức là chỉ xét một chuỗi duy nhất).
Dùng hàm strtok cùng lúc cho 2 chuỗi thì kết quả không như ý là lẽ đương nhiên



Thấy các bác tranh cãi về cái strtok nhiều quá. Tự tay em cài một cái tương tự nè. Tested in VC++ 6.0




#include
#include
#include
#include

char *strtoken(const char *pszInput,const char *pszDelimited)
{
//Khoi tao cho bien static khi chay lan dau bien token se chua xau trong tu can
//tach là token duoc khoi tao khi khai bao cho nen chi co validate 1 lan
static const char *pstr=NULL;
if (pszInput!=NULL) pstr=pszInput;
//Neu xau dua vao khac NULL
if (pstr!=NULL)
{
for (int i=0;ifor (int j=0;jif (pstr[i]==pszDelimited[j])
{
char *token=(char*)malloc(i+1);
memset(token,'\0',i+1);
strncpy(token,pstr,i);
//Tang dia chi cua bien noi tai pstr len i+1 doan
pstr+=(i+1);

//Chinh them phat nua
for (int k=0;kwhile (pstr[0]==pszDelimited[k])
{
pstr++;
k=0;
}

return token;
}
}
return NULL;
}


void main()
{
char *str="Ngo Cong Huan,++,-- Hoan, ";
char *delim=" +- -,";
char *token=strtoken(str,delim);
printf("len %s : %d\n",token,strlen(token));
while (token!=NULL)
{
token=strtoken(NULL,delim);
if (token!=NULL)
printf("len %s : %d\n",token,strlen(token));
}
getch();
}

[C/C++]Hàm strtok cắt xâu – function strtok in string

Tháng Năm 23, 2015 nguyenvanquan7826 Lập trình 25 responses

Hàm strtok(s1,s2) trả về chuỗi đầu tiên sau khi cắt s1 bởi các ký tự có trong chuỗi s2.
VD s1 = “nguyen, van quan” và ta dùng: char *p = strtok(s1,”, “) (có dấu phẩu và dấu cách) thì p là chuỗi: nguyen. Nếu muốn cắt tiếp chuỗi thì ta dùng strtok(NULL,s2) khi đó sẽ cắt chuỗi ban đầu bắt đầu từ vị trí mà trước đó đã dừng lại.
Ta sử dụng vào bài toán sau: nhập vào 1 xâu, sau đó viết nguợc lại các từ trong xâu đã nhập (các từ không chứa dấu phẩy, dấu chấm, đấu cách) và mỗi từ trên 1 dòng

	
#include 
#include 
#include 
 
int main()
{
        int index = 0;
        int i;
        char *a = (char *)malloc(100*sizeof(char));
        char **b = (char **)malloc(100*sizeof(char));;
        printf ("Nhap vao chuoi can dao nguoc: ");
        fflush(stdin);
        gets(a);
 
        char *p;
        p = strtok(a, ",. "); //cat chuoi bang cac ky tu ,. va space
        while(p != NULL)
        {
                b[index] = p;
                index++;
                p = strtok(NULL, ",. "); //cat chuoi tu vi tri dung lai truoc do
        }
        for (i = index-1; i>=0; i--) //in ra cac tu theo thu tu dao nguoc
                printf ("\n%s ", b[i]);
        printf ("\n");

        return 0;
}

Bạn có thể sẽ thích:

As of the change in strtok()'s handling of empty strings, it is now useless for scripts that rely on empty data to function.

Take for instance, a standard header. (with UNIX newlines)

http/1.0 200 OK\n
Content-Type: text/html\n
\n
--HTML BODY HERE---

When parsing this with strtok, one would wait until it found an empty string to signal the end of the header. However, because strtok now skips empty segments, it is impossible to know when the header has ended.
This should not be called `correct' behavior, it certainly is not. It has rendered strtok incapable of (properly) processing a very simple standard.

This new functionality, however, does not affect Windows style headers. You would search for a line that only contains "\r"
This, however, is not a justification for the change.

As of the change in strtok()'s handling of empty strings, it is now useless for scripts that rely on empty data to function.

Take for instance, a standard header. (with UNIX newlines)

http/1.0 200 OK\n
Content-Type: text/html\n
\n
--HTML BODY HERE---

When parsing this with strtok, one would wait until it found an empty string to signal the end of the header. However, because strtok now skips empty segments, it is impossible to know when the header has ended.
This should not be called `correct' behavior, it certainly is not. It has rendered strtok incapable of (properly) processing a very simple standard.

This new functionality, however, does not affect Windows style headers. You would search for a line that only contains "\r"
This, however, is not a justification for the change.