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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀