duanhe1903 2018-08-20 02:45
浏览 91
已采纳

使用strstr时,php if语句不起作用

$test123 = "<ssl_result_message>APPROVAL</ssl_result_message>";

$value = strstr($test123, "<ssl_result_message>"); //gets all text from <ssl_result_message> forward

$value = strstr($value, "</ssl_result_message>", true); //gets all text before </ssl_result_message>

echo $value,"<br>";

if ($value == 'APPROVAL') {
echo 'Transaction is APPROVED!';}


else{
echo 'Transaction is DECLINED';}

The echo value part echos "APPROVAL" just like I expected it to. But the if statement is not working. I keep getting mixed results. I have tried = == and === .

If i use $value = "APPROVAL"; instead of the test123 with strstr and test, it works as expected. What am I missing here?

  • 写回答

2条回答 默认 最新

  • dongtaoxue4674 2018-08-20 03:22
    关注

    Per your debugging message, string(28) "APPROVAL" your string is:

    <ssl_result_message>APPROVAL
    

    not APPROVAL, your browser probably is hiding the element. You could either strip the elements if that is really what your complete string is:

    $value = strip_tags($test123);
    

    or you could use a parser:

    $dom = new DOMDocument;
    libxml_use_internal_errors(true);
    $dom->loadHTML($test123);
    libxml_clear_errors();
    $ssl = $dom->getElementsByTagName('ssl_result_message');
    $value = $ssl->item(0)->nodeValue;
    

    As for an explanation for what your current code did...

    The function is defined to

    Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

    So your searched for <ssl_result_message>, found it and returned that and the remaining string:

    <ssl_result_message>APPROVAL</ssl_result_message>
    

    you then took off the </ssl_result_message> which left you with the 28 character string. A parser is really the best way to handle these type of processes going forward.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败