Jump to content


Photo

substr() Whats wrong?


  • Please log in to reply
6 replies to this topic

#1 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 19 September 2006 - 10:56 PM

I have the following:

<?php
$string = "TI-493";

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

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

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 September 2006 - 04:28 PM

I still haven't figured this out...

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 September 2006 - 04:31 PM

Works fine for me. I get "Woo Right". Is it not working for you? Could you explain what you are aiming to do.

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 September 2006 - 04:41 PM

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

#5 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 September 2006 - 05:05 PM

This is the block of code that deals with a $_POST;
<?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++;
			}
?>

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

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.


#6 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 20 September 2006 - 06:43 PM

I used
echo gettype($sn);
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???

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 September 2006 - 09:20 PM

can you print_r($_POST['sn'];




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users