<?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();
?>
问题出在前两个函数上——它们随机找不到下划线字符。事实上,我调用了超过 50 次脚本以获得正确的结果。添加和 preg_match() 测试但只是为了知道我不确定它是否有有效的正则表达式。