joshrt24
joshrt24

Reputation: 33

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. 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?

Upvotes: 1

Views: 86

Answers (2)

user3783243
user3783243

Reputation: 5224

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.

Upvotes: 2

Rinsad Ahmed
Rinsad Ahmed

Reputation: 1933

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';}

Upvotes: 1

Related Questions