今天热门
热点:

windows下用c语言开发PHP扩展时,在C语言里把字符串转成utf-8编码再打印的方法,phputf-8


因我是用windows下用c开发PHP扩展时,
想在C的函数内直接打印php_printf某字符串(其中含有汉字字符),
但在PHP调用这个函数时,看到的时汉字乱码。PHP页面用的是utf-8编码格式。
代码如下:

hello.c(只是被PHP调用函数,注意不用返回字符串,直接用php_printf打印。)
PHP_FUNCTION(hello)
{
char *hello="";
int len = 0;
char *ret = "";

hello = "中国hello world";
len = strlen(hello);
ret=(char*)emalloc(len);
memcpy(ret, hello, len+1);

php_printf("%s",ret);

RETURN_LONG(0);

}

hello.php
<?php header("Content-type:text/html;charset-utf-8"); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>

<?php hello();?><!--或者非得要用函数返回字符串,再在这里转成utf-8吗?-->

</body>
</html>

如果把浏览器调成GB2312编码时,看到PHP页面的汉字字符串显示就正常了。
所以我认为C中php_printf时是GB2312编码,不知道是不是这个编码格式。
请高手帮我改改,尽量详细些,因我多年没用C了。

谢谢!


解决方案

希望这个有帮助:http://hi.baidu.com/luckymouse2009/blog/item/08fd3323385003ad4723e874.html

www.zrccd.nettrue/topics/20180111/59833.htmlTechArticlewindows下用c语言开发PHP扩展时,在C语言里把字符串转成utf-8编码再打印的方法,phputf-8 因我是用windows下用c开发PHP扩展时, 想在C的函数内直接打印php_printf某字符串(其中含有汉字字符)...

相关文章

    暂无相关文章

用户评论

大家都在看