-----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 "<br><br>With RegEx: $RegExIn";

$RegExPattern = "/(\d{1,3})(,)(\d{1,3})([a-z]{1,2})(.*)/i";

echo "<br>RegEx ~$RegExPattern~";

$bValidity = $iFound

= preg_match( $RegExPattern, $RegExIn, $aRegExOut );

if ( FALSE === $bValidity )

{

$aLocated = NULLSTRING;

echo '<br>Error interpreting RegEx';

}

if ( 0 == $iFound )

{

$aLocated = NULLSTRING;

}

if ( 0 < $iFound )

{

echo "<br>First number =" . $aRegExOut[1];

echo "<br>Separator =" . $aRegExOut[2];

echo "<br>Second number =" . $aRegExOut[3];

echo "<br>Text unit =" . $aRegExOut[4];

echo "<br>Additional non-text =" . $aRegExOut[5];

$value = $aRegExOut[1] . "." . $aRegExOut[3];

echo "<br>Value = $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

Reply via email to