-1

As you see in output, stripos can't catch _formtoken value, anyone can explain it to me with solution :

$value = ['_formtoken','expiry','version','pan','expiry','purchAmount','_formtoken','pan'];
for($i=0;$i<count($value);$i++){

  if (stripos($added, $value[$i]) == false) { 
      echo $value[$i] . ' => Not exists in <br>';    
      $added .= $value[$i];
  } 
  else { 
          echo $value[$i] . ' => already exists  <br>'; 
  } 

}

OUTPUT :

_formtoken => Not exists in
expiry => Not exists in
version => Not exists in
pan => Not exists in
expiry => already exists
purchAmount => Not exists in
_formtoken => Not exists in
pan => already exists 
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ely
  • 13
  • 6

2 Answers2

1

stripos may return 0 or false. If the string you are searching for is at the beginning of the search string, it will return 0. If you use this in an if statement it will evaluate to false. To fix this, use ===.

$value = ['_formtoken','expiry','version','pan','expiry','purchAmount','_formtoken','pan'];
$added = null;
for($i=0;$i<count($value);$i++){
  $pos = stripos($added, $value[$i]);
  if ( $pos === false) { 
      echo $value[$i] . ' => Not exists in <br>' . "\n";    
      $added .= $value[$i];
  } 
  else { 
      echo $value[$i] . ' => already exists  <br>'."\n"; 
  } 
}

When I run it, this is the output I see.

_formtoken => Not exists in <br>
expiry => Not exists in <br>
version => Not exists in <br>
pan => Not exists in <br>
expiry => already exists  <br>
purchAmount => Not exists in <br>
_formtoken => already exists  <br>
pan => already exists  <br>
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • stripos doesnt detecte the value inside text, test this code to see $value = ['_formtoken','=333&version=2.0&pan=4552945074567017&expiry=2211&purchAmount=150&exponent=2&description=Donation&_formtoken=']; for($i=0;$i Not exists in
    '; $added .= $value[$i]."&"; } else { echo $value[$i] . ' => exists
    '; } }
    – Ely Dec 13 '19 at 20:59
  • Are you suggesting that stripos does not work as documented? – ryantxr Dec 13 '19 at 21:37
  • I ran your code snippet. Looks fine to me – ryantxr Dec 13 '19 at 21:37
  • You can see here: https://repl.it/repls/YellowCrazyRoutes – ryantxr Dec 13 '19 at 21:38
  • Check this screenshot : https://i.paste.pics/49cfafc699f83b28110c2cff6d5b6115.png stripos cant catch it inside text. – Ely Dec 14 '19 at 12:58
  • @ElarbiDafrouillah because `_formtoken=7f9D&_formtoken=4ee3` is not a substring of `_formtoken`. – gre_gor Dec 15 '19 at 04:16
  • The problem have been solved by using Regex. Thanks – Ely Dec 16 '19 at 19:25
-1

It looks as though you're trying to create a single string of each of the unique values within your array, is that correct? If so, try this instead, it's far cleaner:

$inputArray = ['_formtoken', 'expiry', 'version', 'pan', 'expiry', 'purchAmount', '_formtoken', 'pan'];

// Only keep the first occurrence of each value
$uniqueInputArray = array_unique($inputArray);

// Output the values separated by a single space between each
echo implode(' ', $uniqueInputArray);

Or as a single line:

echo implode(' ', array_unique(['_formtoken', 'expiry', 'version', 'pan', 'expiry', 'purchAmount', '_formtoken', 'pan']));
Aran
  • 2,429
  • 1
  • 19
  • 19