duanhe1903 2018-08-19 18: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-19 19: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部