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

使用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?

图片转代码服务由CSDN问答提供 功能建议

  $ test123 =“&lt; ssl_result_message&gt; APPROVAL&lt; / ssl_result_message&gt;”; 
 
 $ value  = strstr($ test123,“&lt; ssl_result_message&gt;”);  //从&lt; ssl_result_message&gt;获取所有文本 forward 
 
 $ value = strstr($ value,“&lt; / ssl_result_message&gt;”,true);  //获取&lt; / ssl_result_message&gt; 
 
echo $ value之前的所有文字,“&lt; br&gt;”; 
 
if($ value =='APPROVAL'){
echo'交易被批准!';}  n 
 
else {
echo'Transaction is DECLINED';} 
   
 
 

echo值部分echos“APPROVAL”就像我预期的那样。 但if语句不起作用。 我的结果好坏参半。 我试过= ==和===。

如果我使用$ value =“APPROVAL”; 而不是使用strstr和test的test123,它按预期工作。 我在这里缺少什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论
  • dtd793353 2018-08-20 03:02

    try using strip_tags instead of strstr. Also, use strcmp for string comparision

    $value = strip_tags($test123);
    
    if (strcmp(trim($value),'APPROVAL')==0) {
       echo 'Transaction is APPROVED!';}
    else{
       echo 'Transaction is DECLINED';}
    
    打赏 评论

相关推荐 更多相似问题