php 占位符问题?

查看:60
本文介绍了php 占位符问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

为什么我echo出来的东西就是一个%s?而不是我赋值的字符串?

完整代码:
IndexAction.class.php

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
        $this->responseMsg();
    }
    public function responseMsg(){
        $postStr="<xml>
                 <ToUserName><![CDATA[toUser]]></ToUserName>
                 <FromUserName><![CDATA[fromUser]]></FromUserName>
                 <CreateTime>1348831860</CreateTime>
                 <MsgType><![CDATA[text]]></MsgType>
                 <Content><![CDATA[tel]]></Content>
                 <MsgId>1234567890123456</MsgId>
                 </xml>";
        $postObj = simplexml_load_string($postStr);
        $fromUsername = $postObj->FromUserName;
        //echo $fromUsername;
        //echo '111';
        $toUsername = $postObj->ToUserName;
        $time = time();
        $msgType = $postObj->MsgType;
        $content = $postObj->Content;
        if($content=='tel'){
            $indexModel=new IndexModel();
            $indexModel->responseText($postObj,$content);
        }
    }
}

IndexModel.class.php

<?php
    class IndexModel extends Model{
        //回复纯文本
        public function responseText($postObj,$content) {
            $time=time();
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MSsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>"; 
            echo  "textTpl is: ".$textTpl;
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $content); 
              //  echo $resultStr; 
        }
        
    }
?>

解决方案

浏览器F12去看,多余的xml代码就可以看到了。

这篇关于php 占位符问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆