简体   繁体   English

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

[英]php if statement is not functioning when using strstr

$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. 回声值部分回应“APPROVAL”就像我预期的那样。 But the if statement is not working. 但if语句不起作用。 I keep getting mixed results. 我的结果好坏参半。 I have tried = == and === . 我试过= ==和===。

If i use $value = "APPROVAL"; 如果我使用$ value =“APPROVAL”; instead of the test123 with strstr and test, it works as expected. 而不是使用strstr和test的test123,它按预期工作。 What am I missing here? 我在这里错过了什么?

Per your debugging message, string(28) "APPROVAL" your string is: 根据您的调试消息, string(28) "APPROVAL"您的字符串是:

<ssl_result_message>APPROVAL

not APPROVAL , your browser probably is hiding the element. 不是APPROVAL ,您的浏览器可能隐藏了该元素。 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. 返回haystack字符串的一部分,从第一次出现的针开始到包括haystack结尾。

So your searched for <ssl_result_message> , found it and returned that and the remaining string: 所以你搜索了<ssl_result_message> ,找到了它并返回了剩下的字符串:

<ssl_result_message>APPROVAL</ssl_result_message>

you then took off the </ssl_result_message> which left you with the 28 character string. 然后你取下了</ssl_result_message> ,它为你留下了28个字符串。 A parser is really the best way to handle these type of processes going forward. 解析器确实是处理这类过程的最佳方法。

try using strip_tags instead of strstr. 尝试使用strip_tags而不是strstr。 Also, use strcmp for string comparision 另外,使用strcmp进行字符串比较

$value = strip_tags($test123);

if (strcmp(trim($value),'APPROVAL')==0) {
   echo 'Transaction is APPROVED!';}
else{
   echo 'Transaction is DECLINED';}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM