Weird behaviour when searching for char in array string elements

90 Views Asked by At
<?php

ini_set('error_reporting', '-1');
ini_set('display_errors', '1');
ini_set('apc.enabled', '0');
gc_enable();

$array = array("php", "php_php", "php_php", "php_php", "php");
$arraysize = count($array);
$style = " style='border: 1px solid black;'";
$strcmpcharcount = 0;
$equalcmpcharcount = 0;

foreach ($array as $key)
{
  $strcmpcharcount = 0;
  $equalcmpcharcount = 0;

  if (strstr($key, "_") !== false)
  {
    $strstr[] = "found";
    $explodedstring1[] = explode("_", $key);
  }
  else
  {
    $strstr[] = "not found";
    $explodedstring1[] = "not found";
  }

  if (strpos($key, "_") !== false)
  {
    $strpos[] = "found";
    $explodedstring2[] = explode("_", $key);
  }
  else
  {
    $strpos[] = "not found";
    $explodedstring2[] = "not found";
  }

  if (preg_match("/[^_+$]/", $key))
  {
    $preg_match[] = "found";
    $explodedstring3[] = explode("_", $key);
  }
  else
  {
    $preg_match[] = "not found";
    $explodedstring3[] = "not found";
  }

  $keysize = strlen($key);
  for ($i = 0; $i < $keysize; $i++)
  {
    if (strcmp($key[$i], "_") === 0) { $strcmpcharcount++; }
  }

  for ($j = 0; $j < $keysize; $j++)
  {
    if ($key[$j] === "_") { $equalcmpcharcount++; }
  }

  if ($strcmpcharcount > 0)
  {
    $strcmp[] = "found";
    $explodedstring4[] = explode("_", $key);
  }
  else
  {
    $strcmp[] = "not found";
    $explodedstring4[] = "not found";
  }

  if ($equalcmpcharcount > 0)
  {
    $equalcmp[] = "found";
    $explodedstring5[] = explode("_", $key);
  }
  else
  {
    $equalcmp[] = "not found";
    $explodedstring5[] = "not found";
  }
}
echo "<table$style>
<th$style>
<tr>
<td$style>strstr()</td>
<td$style>strpos()</td>
<td$style>preg_match()</td>
<td$style>strcmp()</td>
<td$style>'==='</td>
</tr>
</th>";
for($k = 0; $k < $arraysize; $k++)
{
  echo "<tr>
  <td$style>$strstr[$k]</td>
  <td$style>$strpos[$k]</td>
  <td$style>$preg_match[$k]</td>
  <td$style>$strcmp[$k]</td>
  <td$style>$equalcmp[$k]</td>
  </tr>";
}
echo "</table>";

exit();

?>

The problem is with first two functions - they randomly fails to find the underscore char. In fact I called more than 50 times the script to got proper results. Added and preg_match() test but just to know I'm not sure if it has valid regex.

1

There are 1 best solutions below

1
On

You're adding new elements to $strstr, $strpos tables and so on and in the end you do for... and printing from these tables where $k keys don't necessarily have to exists.

Check var_dump or print_r on these tables and you will see that in fact they got elements, but their indexes aren't matched with $array indexes (and i guess that's what you want to achieve).

You can change foreach ($array as $key) to foreach ($array as $index => $key) and all occurences like $strstr[] = "found"; to $strstr[$index] = "found"; (also for other recognision methods) and then run the script again to see results.

In last block (for($k = 0; $k < $arraysize; $k++)...) you should either validate if $strstr[$k] (and other arrays) exists before printing it, or print these arrays separately by foreach.

You can also make one table for results and make it multidimensional with function names as keys in the first level and put results in there.