Jump to content

Archived

This topic is now archived and is closed to further replies.

SharkBait

substr() Whats wrong?

Recommended Posts

I have the following:

[code]
<?php
$string = "TI-493";

if(substr(strtolower($string), 0, 3) != "ti-") {
  echo "Error Wrong";
} else {
  echo "Woo Right";
}
?>
[/code]

Am I doing this wrong with substr() should I be using something else?

Share this post


Link to post
Share on other sites
Works fine for me. I get "Woo Right". Is it not working for you? Could you explain what you are aiming to do.

Share this post


Link to post
Share on other sites
I am looking to check the first 3 characters of a string a user inputs to see if they at least match. After the first 3 characters i dont care about

Share this post


Link to post
Share on other sites
This is the block of code that deals with a $_POST;
[code]
<?php
foreach($_POST['sn'] as $sn) {
$sn = mysql_real_escape_string(trim($sn));
$pre = substr(strtolower($sn),0,3);
if ($pre != "tr-") {
echo "{$pre} <br />";
$errMsg .= "<li>The Serial Number must follow the TR- scheme.</li>";
break;
} else {
echo "AGHHFDF";
}
if ($sn <> "") {
$strqry = "SELECT * FROM rebates WHERE sn = '{$sn}'";
$query = mysql_query($strqry) or die("MySQL Error: Aborting Query");
$found = mysql_num_rows($query);
if($found > 0) {
$FOUNDSN[$b] = $sn;
$SNFAILED = true;
} else {
$FOUNDSN[$b] = "";
}

$i++;
} else {
$FOUNDSN[$b] = "";
}
$b++;
}
?>
[/code]

Now it never echos the 'Pre is {$pre}' but it does assign [code=php:0] $errMsg .= "<li>The Serial.....</li>";[/code]

It does echo the AGHHFDF so I am completely confused as to what is happening.  Does it have to do with the break; I have?  I want the foreach loop to stop on this if statement.

Share this post


Link to post
Share on other sites
I used [code=php:0]echo gettype($sn);[/code] and it returned as a boolean type (which is what php.net says about substr()))

So in the above code, am I really compairing a boolean to a string? Or am I right to think that its compairing a boolean to the integer value of "tr-" ?

Am I comparing strings in an incorrect way???

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.