【问题标题】:Make file_get_contents return only what i want让 file_get_contents 只返回我想要的
【发布时间】:2014-06-16 09:17:00
【问题描述】:

我有这个代码。我希望在每个职位上都可以访问。但我得到的结果是字符串。这是一个例子:

"string(7652)"{"id":"34","nume":"STUDENT","bilet":null,"log":"2014-06-16 11:41:28\n2014- 06-16 11:41:30\n2014-06-16 11:41:41\n2014-06-16 11:41:41\n2014-06-16 11:41:43\n2014-06-16 11:42 :03\n2014-06-16 11:42:05\n2014-06-16 11:42:06\n2014-06-16 11:42:06\n2014-06-16 11:42:06\n2014-06 -16 11:42:09\n2014-06-16 11:42:11\n2014-06-16 11:42: "

我只需要日志......所以我可以用这些日志制作一个 heml 表。想法?

public $datte = array();
public function aduDpServer(){
    $data = array ('foo' => 'bar', 'id' => 34); 
    $data = http_build_query($data); 
    $context_options = array ( 
        /*'http' => array ( 
            'method' => 'POST', 
            'header'=> "Content-type: application/x-www-form-urlencoded\r\n" 
            . "Content-Length: " . strlen($data) . "\r\n", 
            'content' => $data 
        ) */
        'http'=> array(
            'method'=>'POST',
            'header'=>"Content-type: application/x-www-form-urlencoded\r\n",
            'content'=>$data
        )
    ); 
    $context = stream_context_create($context_options);
    $this->datte = file_get_contents('http://www.jajaja/x/updt.php', false, $context); 

    //var_dump($fp);

}

public function afiseaza(){
    //print_r($this->datte);
    echo"<table border=1 cellpading=0 cellspacing=0 >";
    echo"<th>Accesari</th>";
    $this->datte = is_array($this->datte) ? $this->datte: array($this->datte); //fortare vector
    foreach ($this->datte as $d) {
        echo"<tr>";
        echo"<td>".$d."</td>";echo"<br>";
        echo"</tr>";

    }

}

【问题讨论】:

  • 你需要循环遍历$this-&gt;datte['log'];
  • 怎么样?因为我直接在我的数组 datte 中获得了所有字符串。
  • 这是一个 json 字符串,您需要将其解码为数组。

标签: php file-get-contents


【解决方案1】:

您的字符串是 JSON,因此您可以使用 json_decode 将您的数据转换为 php 数组。

$this->datte = json_decode(file_get_contents('http://www.jajaja/x/updt.php', false, $context), true); 

【讨论】:

  • 不工作...当我尝试 var_dump ($this->datte) 我得到 NULL
  • 检查您的数据是否为有效的 JSON。参见示例 #3:php.net/manual/en/function.json-decode.php
  • 我使用了explode ..我看到我提出了我真的不知道的问题并且我得到了-rep。很高兴了解这些的人来到这里而不是支持我们......给予-。谢谢stackoverflow!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多