Re: substr?
| From: | Chris Shiflett | Date: | Tue, 18 Jun 2002 03:20:16 +0000 |
| Subject: | Re: substr? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-102639@lists.php.net to get a copy of this message | ||
You need to just make sure you know what you're testing and what logical group your NOT (!) is applied to.
if (!substr($string, 0, -7) == "083")I'm not sure here, but I think you're just wanting to make sure everything except the last seven characters is not the string "083". If this is correct, just simplify (and correct) your logic with something like the following: if (substr($string, 0, -7) != "083") Or, better yet, since you really want the first three characters, just say so: if (substr($string, 0, 3) != "083) Not only will you find yourself having less trouble by keeping things simple, you'll find your code is much cleaner and more maintainable. Hope that helps. Chris Chris Knipe wrote:
Hi all, Is this right? It seems to me that substr is working in reverse? $string = "1234567890"; // Always numerical, always 10 chars. if (!substr($string, 0, -7) == "083") { echo "not 083\n"; } else { // Exists here. echo "is 083\n"; } Due to the reverse if, shouldn't it exit not 083? I'd just like to get some clarity on this, allot of my work depends on validating these numbers *correctly*.... Basically, I want to see if the first three characters is 072, 073, 082, 083, or 084. If not, the $string must be discarded. I've also tried if(substr($string, 0, -7) != "083"), but that also doesn't work.... ereg(*) also doesn't seem to be able to search for patterns only on the beginning of a string, so even if I wanted to use a ereg expression, I'd have to cut the first 3 chars from the string with substr first? How can / should I validate these numbers?