bstr(s_time, 0, time_l - 0), -1);
hour = atoi(s_num); free(s_num);
if(time_l != time_r)
{
return_if_fail(s_num = substr(s_time, \
time_l + 1, time_r - time_l - 1), -1);
minute = atoi(s_num); free(s_num);
return_if_fail(s_num = substr(s_time, \
time_r + 1, strlen(s_time) - time_r - 1), -1);
second = atoi(s_num); free(s_num);
}
else
{
return_if_fail(s_num = substr(s_time, \
time_l + 1, strlen(s_time) - time_l - 1), -1);
minute = atoi(s_num); free(s_num);
second = 0;
}
free(s_time);
sprintf(datetime, "%04d-%02d-%02d %02d:%02d:%02d",
year, month, day, hour, minute, second);
return 0;
}
char* substr(const char* str, int pos, int len)
{
if(len <= 0)
{
printf("[*ERROR]: %s: %d: str = [%s], pos = [%d], len = [%d]\n", \
__FILE__, __LINE__, str, pos, len);
return NULL;
}
char *sp = (char*) malloc(len + 1);
sp[len] = 0;
int i = 0;
for(; i < len; i++) {
sp[i] = str[pos + i];
}
return sp;
}
int find(const char *src, const char *str)
{
int i = 0;
int j = 0;
int n_len_src = strlen(src);
int n_len_str = strlen(str);
int n_pos = 0;
for(i = 0 ; i <= n_len_src - n_len_str; i++) {
n_pos = i;
for(j = 0 ; j < n_len_str ; j++) {
if(*(src + n_pos) != *(str + j)) {
break;
}
if(j == n_len_str - 1) {
return i;
}
n_pos++;
}
}
return -1;
}
int rfind(const char *src, const char *str)
{
int i = 0;
int j = 0;
int n_len_src = strlen(src);
int n_len_str = strlen(str);
int n_pos = 0;
for(i = n_len_src - n_len_str ; i > 0; i--) {
n_pos = i;
for(j = 0 ; j < n_len_str ; j++) {
if(*(src + n_pos) != *(str + j)) {
break;
}
if(j == n_len_str - 1) {
return i;
}
n_pos++;
}
}
return -1;
}
测试示例test.c
/**
* 编译: gcc test.c PhoneDateExtract.c -o test
* 执行: ./test
* 结果:
* format phone = [800-2142-325]
* format datetime = [2011-03-07 14:03:00]
*/
#include "PhoneDateExtract.h"
#include
#include
int main(int argc, char* argv[])
{
// 中国大陆区域通用电话号码提取
char* phone = NULL;
phone_extract("电话:800-2142-325", &phone);
printf("format phone = [%s]\n", phone);
free(phone);
// 简体中文文本网页时间提取
char* datetime = NULL;
datetime_extract("2011年3月7日 14:03", &datetime);
printf("format datetime = [%s]\n", datetime);
free(datetime);
return 0;
}
资源下载:http://up.2cto.com/2011/1010/20111010103218741.zip