pabs1983 Posted November 14, 2007 Share Posted November 14, 2007 Hi guys, First post here. Straight into it. I am wondering if i can get some help. I know how to use the IF statement for a full string, however i am looking for it to check the start of a string. It is a project for work, where there are files that come in, these files start with 3 letters and a number followed by a : sign. each department has a different combination and i would it to be able to read that first part and know what department it is. Is this possible? Quote Link to comment Share on other sites More sharing options...
trq Posted November 14, 2007 Share Posted November 14, 2007 <?php $s = 'dep334455'; if (substr($s,0,2) == 'dep') { echo "belongs to dep"; } ?> Quote Link to comment Share on other sites More sharing options...
eon201 Posted November 14, 2007 Share Posted November 14, 2007 hey, you could use the strpof function to locate characters in a sting for instance $find = 'whatyouarelookingfor; if (strpos($whatyourlookingin, $find) == true){ //do stuff in here } else{//do stuff in here} I think that should work. I use the same method in one of my scripts. If it doesnt work post your code and the output for us to see! eon201 Quote Link to comment Share on other sites More sharing options...
Adam Posted November 14, 2007 Share Posted November 14, 2007 if (??? == substr($str,0,3)) $str's value will not change after using substr() unless you set $str as substr(...) : $str = substr($str,0,3); - Adam Quote Link to comment Share on other sites More sharing options...
pabs1983 Posted November 14, 2007 Author Share Posted November 14, 2007 Some of the departments codes are longer than others (some have 4 letters, some have 3, etc) can i not just check all the characters before ':' ? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 try <?php $s = 'dep334455'; if (preg_match('/^dep/i',$s)) { echo "belongs to dep"; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted November 14, 2007 Share Posted November 14, 2007 Your probably going to need to be a little more specific in the description of what exactly these strings look like, but maybe.... <?php $s = 'dep2345:'; preg_match('/^(.*):/',$s,$matches); if ($matches[0] == 'dep2345') { echo "belongs to dep"; } ?> helps. Quote Link to comment Share on other sites More sharing options...
pabs1983 Posted November 14, 2007 Author Share Posted November 14, 2007 the strings look like 'xxx1234:filename.xxx' however, it could also have more or less characters in the first section so it could be 'xxxxx12345:...' Quote Link to comment Share on other sites More sharing options...
pabs1983 Posted November 14, 2007 Author Share Posted November 14, 2007 This is what i have so far: <?php $s = $_REQUEST['file]; if (preg_match('/^ITS0001/i',$s)) echo "<meta http-equiv=refresh content='1;url=Department/ITSales.php?file="; echo $_REQUEST['file']; echo "' />"; elseif (preg_match('/^PERS0001/i',$s)) echo "<meta http-equiv=refresh content='1;url=Department/Personnel.php?file="; echo $_REQUEST['file']; echo "' />"; else echo "<h1>Not Recognised Format</h1>"; ?> This works for ITS0001 file, but not for PERS0001. Instead i get an error 'Parse error: syntax error, unexpected T_ELSEIF in /home/www/XXXXXXXXXX/target.php on line 16' Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 please put your could in blocks like the following <?php $s = $_REQUEST['file]; if (preg_match('/^ITS0001/i',$s)) { echo "<meta http-equiv=refresh content='1;url=Department/ITSales.php?file="; echo $_REQUEST['file']; echo "' />"; } elseif (preg_match('/^PERS0001/i',$s)) { echo "<meta http-equiv=refresh content='1;url=Department/Personnel.php?file="; echo $_REQUEST['file']; echo "' />"; } else { echo "<h1>Not Recognised Format</h1>"; } ?> Quote Link to comment Share on other sites More sharing options...
pabs1983 Posted November 14, 2007 Author Share Posted November 14, 2007 WORKS PERFECTLY CHEERS GUYS Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.