[PHP] Is there a better way of doing this?
Hello all, You have been lots of help in the past, so I was wondering if you could help me out with this, basically I just have to parse a string similar to 4,31m) figure out if it is numeric or not, then if it is remove the last to characters on the string (the 'm)') and change the string so that it is 4.31. There is probably a better way of doing this but I can't figure out how to make it better or more efficient $tim = 3,43m); // take into account that the last 2 characters may not be\ 'm)' like we are expecting, and store them in a var $last2ontime = substr($tim,strlen($tim),strlen($tim)-3); $tim = str_replace(,,,$tim); // get rid of the , in the time (i.e. time = 3,43 and we want it to be 343) $tim = substr($tim,0,strlen($tim)-2); if(is_numeric($tim)){ if($debug){ print nl2br( Time is Numeric!\n ); } $t = substr($tim,0,strlen($tim)-2); $te = substr($tim,strlen($tim)-2); if($debug){ print nl2br( T = $t\nte = $te\n ); } if(is_null($t) or $t == ){ $tim = $te[0] ..0. $te[1]; }else{ $tim = (string)$t ... (string)$te .m); } }else{ if($debug){ print got else!; } Thank You, Mike [EMAIL PROTECTED] [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there a better way of doing this?
Hello Mike, parse a string similar to 4,31m) figure out if it is numeric or not, then if it is remove the last to characters on the string (the 'm)') and change the string so that it is 4.31. There is probably a better way of doing this but I can't figure out how to make it better or more efficient $tim = 3,43m); // take into account that the last 2 characters may not be\ 'm)' like we are expecting, and store them in a var $last2ontime = substr($tim,strlen($tim),strlen($tim)-3); $tim = str_replace(,,,$tim); // get rid of the , in the time (i.e. time = 3,43 and we want it to be 343) $tim = substr($tim,0,strlen($tim)-2); if(is_numeric($tim)){ if($debug){ print nl2br( Time is Numeric!\n ); } $t = substr($tim,0,strlen($tim)-2); $te = substr($tim,strlen($tim)-2); if($debug){ print nl2br( T = $t\nte = $te\n ); } if(is_null($t) or $t == ){ $tim = $te[0] ..0. $te[1]; }else{ $tim = (string)$t ... (string)$te .m); } }else{ if($debug){ print got else!; } Add the following code to the above (NB think you're a } short): $RegExIn = 3,43m); echo brbrWith RegEx: $RegExIn; $RegExPattern = /(\d{1,3})(,)(\d{1,3})([a-z]{1,2})(.*)/i; echo brRegEx ~$RegExPattern~; $bValidity = $iFound = preg_match( $RegExPattern, $RegExIn, $aRegExOut ); if ( FALSE === $bValidity ) { $aLocated = NULLSTRING; echo 'brError interpreting RegEx'; } if ( 0 == $iFound ) { $aLocated = NULLSTRING; } if ( 0 $iFound ) { echo brFirst number = . $aRegExOut[1]; echo brSeparator = . $aRegExOut[2]; echo brSecond number = . $aRegExOut[3]; echo brText unit = . $aRegExOut[4]; echo brAdditional non-text = . $aRegExOut[5]; $value = $aRegExOut[1] . . . $aRegExOut[3]; echo brValue = $value; } Totally unsure about $last2ontime. The RegEx has been structured fairly loosely because I'm not sure of all the specifications for how many digits can be in the various places, etc; so feel free to altered the repetition values (inside the curly brackets) to suit. Of course, if the comma is missing or there is no integer value, (etc), then the whole thing will fall apart and give you nothing (hence the $aLocated = NULLSTRING; line which should probably be reworded/tied back to $value (yes I was reusing old code))! Hope it helps, =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Is there a better way of doing this?
-Original Message- From: DL Neil [mailto:[EMAIL PROTECTED]] Sent: Wednesday, August 21, 2002 4:56 PM To: Mike; [EMAIL PROTECTED] Subject: Re: [PHP] Is there a better way of doing this? Hello Mike, parse a string similar to 4,31m) figure out if it is numeric or not, then if it is remove the last to characters on the string (the 'm)') and change the string so that it is 4.31. There is probably a better way of doing this but I can't figure out how to make it better or more efficient $tim = 3,43m); // take into account that the last 2 characters may not be\ 'm)' like we are expecting, and store them in a var $last2ontime = substr($tim,strlen($tim),strlen($tim)-3); $tim = str_replace(,,,$tim); // get rid of the , in the time (i.e. time = 3,43 and we want it to be 343) $tim = substr($tim,0,strlen($tim)-2); if(is_numeric($tim)){ if($debug){ print nl2br( Time is Numeric!\n ); } $t = substr($tim,0,strlen($tim)-2); $te = substr($tim,strlen($tim)-2); if($debug){ print nl2br( T = $t\nte = $te\n ); } if(is_null($t) or $t == ){ $tim = $te[0] ..0. $te[1]; }else{ $tim = (string)$t ... (string)$te .m); } }else{ if($debug){ print got else!; } Add the following code to the above (NB think you're a } short): $RegExIn = 3,43m); echo brbrWith RegEx: $RegExIn; $RegExPattern = /(\d{1,3})(,)(\d{1,3})([a-z]{1,2})(.*)/i; echo brRegEx ~$RegExPattern~; $bValidity = $iFound = preg_match( $RegExPattern, $RegExIn, $aRegExOut ); if ( FALSE === $bValidity ) { $aLocated = NULLSTRING; echo 'brError interpreting RegEx'; } if ( 0 == $iFound ) { $aLocated = NULLSTRING; } if ( 0 $iFound ) { echo brFirst number = . $aRegExOut[1]; echo brSeparator = . $aRegExOut[2]; echo brSecond number = . $aRegExOut[3]; echo brText unit = . $aRegExOut[4]; echo brAdditional non-text = . $aRegExOut[5]; $value = $aRegExOut[1] . . . $aRegExOut[3]; echo brValue = $value; } Totally unsure about $last2ontime. The RegEx has been structured fairly loosely because I'm not sure of all the specifications for how many digits can be in the various places, etc; so feel free to altered the repetition values (inside the curly brackets) to suit. Of course, if the comma is missing or there is no integer value, (etc), then the whole thing will fall apart and give you nothing (hence the $aLocated = NULLSTRING; line which should probably be reworded/tied back to $value (yes I was reusing old code))! Hope it helps, =dn Sweet! It worked perfectly as I needed it to and I get to remove a lot of extra code from my script. Thank you bunches, Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php