didgydont Posted July 3, 2009 Share Posted July 3, 2009 is it possible to do some thing like this ? $chars = preg_split("/([a-z0-9]{3})([a-z0-9]{2})/", $str); i found this but it gets to messy for what i want to do $v = array(); for($x=0;$x<strlen($str);$x++){ $v[] = $str[$x];} Quote Link to comment Share on other sites More sharing options...
Adam Posted July 3, 2009 Share Posted July 3, 2009 Well yeah it's possible, but what are you trying to do?? Quote Link to comment Share on other sites More sharing options...
didgydont Posted July 3, 2009 Author Share Posted July 3, 2009 make this smaller trying to convert this 530pm to $1 = 5 $2= 30 $3=pm this works for now but i want to make it tiny function b4ampm($b4apptime){ if(ereg("^[a-z0-9]{5}$", $b4apptime)){ $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[3] . $b4a[4] ;} if(ereg("^[a-z0-9]{6}$", $b4apptime)) { $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[4] . $b4a[5] ;} } function b4mins($b4apptime){ if(ereg("^[a-z0-9]{5}$", $b4apptime)){ $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[1] . $b4a[2] ;} if(ereg("^[a-z0-9]{6}$", $b4apptime)) { $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[2] . $b4a[3] ;} } function b4hour($b4apptime){ if(ereg("^[a-z0-9]{5}$", $b4apptime)){ $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[0] ; } if(ereg("^[a-z0-9]{6}$", $b4apptime)) { $b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY); return $b4a[0] . $b4a[1] ; } } Quote Link to comment Share on other sites More sharing options...
Adam Posted July 3, 2009 Share Posted July 3, 2009 What are the possible formats it could be? 24hour possible? leading zeros possible? etc. You certainly don't need all that... Perhaps something like: $str = '530pm'; preg_match('/(0?[0-9]|1[0-2])(0?[0-9]|[1-5][0-9])(am|pm)/i', $str, $matches); print_r($matches); Can make it more accurate once we know all the possible formats though... Quote Link to comment Share on other sites More sharing options...
.josh Posted July 3, 2009 Share Posted July 3, 2009 is 530pm really the format you are working with? So what happens when it is 123pm or the like? is it 1:23pm? 12:30pm? 12:03pm? You can't accurately regex something like that. Quote Link to comment Share on other sites More sharing options...
didgydont Posted July 4, 2009 Author Share Posted July 4, 2009 thanx guys all possible formats are like so "900am", "915am", "930am", "945am", "1000am", "1015am", "1030am", "1045am", "1100am", "1115am", "1130am", "1145am", "1200pm", "1215pm" and so on thats why i had if(ereg("^[a-z0-9]{5}$", $b4apptime)) and if(ereg("^[a-z0-9]{6}$", $b4apptime)) i used your help to do this an example $str = "1245pm"; if(ereg("^[a-z0-9]{5}$", $str)){ preg_match('/([0-9]{1})([0-9]{2})(am|pm)/', $str, $matches);} if(ereg("^[a-z0-9]{6}$", $str)){ preg_match('/([0-9]{2})([0-9]{2})(am|pm)/', $str, $matches);} print_r($matches); answer = Array ( [0] => 1245pm [1] => 12 [2] => 45 [3] => pm ) wich will shrink the codes lots but i dont under stand why the array put in the original value ? Quote Link to comment Share on other sites More sharing options...
Adam Posted July 5, 2009 Share Posted July 5, 2009 Realise this is closed now but, you could improve that further: if (strlen($str) == 5) { $pattern = '/([0-9]|1[0-2])(0[0-9]|[1-5][0-9])(am|pm)/'; } elseif (strlen($str) == 6) { $pattern = '/(1[0-2])(0[0-9]|[1-5][0-9])(am|pm)/'; } preg_match($pattern, $str, $matches); Quote Link to comment Share on other sites More sharing options...
didgydont Posted July 12, 2009 Author Share Posted July 12, 2009 thank you very much 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.