PHP:substr 返回空字符串 [英] PHP: substr returns empty string

查看:56
本文介绍了PHP:substr 返回空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PHP 文件中的 substr 函数结果是一个空字符串.

这是我的 PHP 代码:

 获取",'header'=>"用户代理:MKUxA&8PtUYP(d3So)pfPSIvD5cf75"));$stream = stream_context_create($requestArray);$streamResult = file_get_contents('https://beoordelingen.feedbackcompany.nl/samenvoordeel/scripts/flexreview/getreviewxml.cfm?ws=9673&publishIDs=0&nor=0&publishDetails=0&publishDetailScores=0&0&0&sort=desc&foreign=1&v=3&publishDetailScores=1&Basescore=10', false, $stream);$str = substr($streamResult, 0, 3);var_dump($str);echo "
";var_dump($streamResult);echo "
";var_dump($http_response_header);?>

结果如下:

string(3) "string(251) " 8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html "数组(6) { [0]=>string(15) "HTTP/1.1 200 OK" [1]=>string(37) "Content-Type: text/xml; charset=UTF-8" [2]=>字符串(25)服务器:Microsoft-IIS/7.5"[3]=>string(21) "X-Powered-By: ASP.NET" [4]=>string(35) "日期:2016 年 2 月 22 日星期一 13:39:34 GMT" [5]=>字符串(17)连接:关闭"}

谁能告诉我我做错了什么并帮助我?

解决方案

内容正确返回,但您在浏览器中看到上述结果,因为浏览器会解释标签.

您的结果是:

<块引用>

所以,substr($streamResult, 0, 3)<?x,即——渲染——是:

<块引用>

此外,$streamResult 在 XML 之上——呈现——是:

<块引用>

8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html

如果你想在浏览器中看到结果,那么写:

 echo htmlentities( $streamResult );

返回的数据是 XML,所以——要解析它——你必须使用 DOMDocument(或其他解析器):

$dom = new DOMDocument();$dom->loadXML( $streamResult );$score = $dom->getElementsbyTagName('score')->item(0)->nodeValue;回声 $score;

上面的例子会输出:

<块引用>

8.7

The substr function in my PHP file has an empty string as a result.

Here is my PHP code:

    <?php
$requestArray = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-agent: MKUxA&8PtUYP(d3So)pfPSIvD5cf75"
  )
);

$stream = stream_context_create($requestArray);
$streamResult = file_get_contents('https://beoordelingen.feedbackcompany.nl/samenvoordeel/scripts/flexreview/getreviewxml.cfm?ws=9673&publishIDs=0&nor=0&publishDetails=0&publishDetailScores=0&publishOnHold=0&sort=desc&foreign=1&v=3&publishDetailScores=1&Basescore=10', false, $stream);

$str = substr($streamResult, 0, 3);
var_dump($str);

echo "<br>";
var_dump($streamResult);
echo "<br>";
var_dump($http_response_header);

?>

This is the result:

string(3) "string(251) " 8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html "
array(6) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(37) "Content-Type: text/xml; charset=UTF-8" [2]=> string(25) "Server: Microsoft-IIS/7.5" [3]=> string(21) "X-Powered-By: ASP.NET" [4]=> string(35) "Date: Mon, 22 Feb 2016 13:39:34 GMT" [5]=> string(17) "Connection: close" }

Could anyone tell me what I'm doing wrong and help me out?

解决方案

The content is correctly returned, but you see above result in browser because the browser interpret the tags.

Your result is:

<?xml version="1.0" encoding="UTF-8" ?>
    <rating><score>8.7</score><scoremax>10</scoremax><noReviews>9</noReviews><detailslink>https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html</detailslink><reviewDetails></reviewDetails></rating>

So, substr($streamResult, 0, 3) is <?x, that — rendered — is:

Also, $streamResult is above XML that — rendered — is:

8.7109https://beoordelingen.feedbackcompany.nl/NL-NL/De%2DMariannehoeve.html

If you want see the result in the browser, write this:

 echo htmlentities( $streamResult );

Edit:

The returned data is XML, so — to parse it — you have to use DOMDocument (or another parser):

$dom = new DOMDocument();
$dom->loadXML( $streamResult );
$score = $dom->getElementsbyTagName( 'score' )->item(0)->nodeValue;

echo $score;

The example abowe will output:

8.7

这篇关于PHP:substr 返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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