读取html文件并替换c中的字符串

时间:2014-11-08 23:00:52

标签: php c string replace utf-8

我需要函数来读取html文件(电子邮件模板)并替换该文件中的一些字符串。我已经有了一些功能,但我觉得这个功能让html模板在发送电子邮件时看起来很难看。这是我的功能

    int send_formated_mail(char *to, struct mail_struct *mail, char *string)
{
    int retval;
    /* Open pipe to sendmail */
    sendmail_pipe=popen(sendmail_path, "w");
    if(sendmail_pipe == NULL)
    {
        fprintf(stderr, "No Pipe to \"sendmail\"\n");
        return 1;
    }
    FILE *fd = fopen(mail->mail_layout_path, "r");
    if(fd != NULL)
    {
        fprintf(sendmail_pipe,  "To: %s\r\n"
                    "From: %s\r\n"
                    "Reply-to: %s\r\n"
                    "Subject : %s\r\n"
                    "Content-Type: text/html; charset=utf-8\r\n"
                    "Mime-Version: 1.0\r\n\n",
                    to, from, reply_to, mail->subject);
        char buffer[255];
        memset(buffer,'\0',sizeof(buffer));
        while(!feof(fd))
        {
            fread(buffer, sizeof(buffer), 1, fd);
            fprintf(sendmail_pipe, "%s", replace_str(buffer, LINK, string));
            memset(buffer,'\0',sizeof(buffer));
        }
        fflush(sendmail_pipe);
        retval = pclose(sendmail_pipe);
        fclose(fd);
        return retval;
    }
    return 1;
}

char *replace_str(char *str, char *orig, char *rep)
{
    static char buffer[4096];
    char *p;

    if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
        return str;

    strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
    buffer[p-str] = '\0';

    sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

    return buffer;
}

模板是utf-8字符集,我需要发送utf-8。现在字符串被替换但是模板有一些错误,就像charset不同。我知道如何做到这一点是PHP和PHP它运作良好,但我不知道在... ...

欢迎任何帮助!

p.s我可以从这个c函数执行php文件吗?所以我不需要这个替换和charset就可以了。

0 个答案:

没有答案