Reputation: 1273
$cartArray structure:
Array ( [0] => 1. X => 2. Y [2] => 3. Z [3] => 4. XX [4] => 5. YY [5] => )
$list:
$list = array(
"1.",
"2.",
"3.",
"4.",
"5."
);
Function
function alignNum($cartArray, $list)
{
foreach($cartArray as $cA)
{
echo "<br />" . $cA . "<br /><br />";
foreach($list as $l)
{
echo $l . "<br />";
echo "Type of \$l is " . gettype($l) . " and type of \$cA is " . gettype($cA) . "<br />";
$pos = strpos($cA, $l);
echo "Comparing " . $l . " to " . $cA . "<br />";
if ($pos !== true)
{
echo "<br />" . $l . " was not found in " . $cA . "<br /><br />";
continue;
}
else
{
$post_cA = str_replace("EUR", "EUR</span>", $cA);
$new_cA[] = (str_replace($l, substr($l, 0, -4) . "<span style='float: right>'", $cA)) . $post_cA;
return $new_cA;
}
}
}
}
$new_cart = alignNum($cart, $list);
So, I keep getting my custom debug message: "X was not found in Y". I.e it's not founding it X in ANY of the provided strings.
It's not a question of types either. Both are strings.
Any ideas?
EDIT: Also, note that I've censored some stuff out, since this is business related code.
Upvotes: 0
Views: 916
Reputation: 2760
German Arlington is actually right. Have a look what strpos is returning:
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
with
if ($pos !== true)
you are asking wether strpos returned a boolean true (as you are checking typesafe with !==). But actually strpos may have found the needle and returned i.e 2 for the position of the first match, but you asked for true.
try to check the other way round:
if (false === $pos) { // your debug message }
Upvotes: 1
Reputation: 3353
It is generally a bad idea (unless you are C programmer) to compare numeric and boolean even in
if ($pos != true)
but in
if ($pos !== true)
AFAIK it will NEVER match because the variables are different types
Upvotes: 1