Problem mit PHP-Platzhaltern?
给我你的怀抱
给我你的怀抱 2017-05-24 11:31:08
0
3
636

Warum ist das, was aus meinem Echo kommt, ein %s? Anstelle der von mir zugewiesenen Zeichenfolge?

Vollständiger Code:
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; 
        }
        
    }
?>
给我你的怀抱
给我你的怀抱

Antworte allen(3)
大家讲道理

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

淡淡烟草味

浏览器也能解析 XML 的, 查看页面源码就就能看见了

某草草

你本来就是echo的tpl,输出%s没什么不对

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage