Jump to content

parsing numbers help


tjburke79

Recommended Posts

i am wondering if someone can help me out.  i am trying to parse a field, and i need to split the field into 2 values.

 

$Var = "+1.5-165";  or  $Var = "-1.5+145";

 

i need +1.5 as on var and -165 as another, and it is possible that the numbers are switch around, so i can not use the explode function to spit them

 

any body of a solution?

 

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/242879-parsing-numbers-help/
Share on other sites

I'm not sure what you mean by switched around.  This is specific to the first number having a decimal and the second number not having one.  However, preg_match will match both of these, but you'll have to figure out what elements in the match array to grab.  Off the top of my head it would be match[3] and match[5] but you should var_dump and test.

 

$regex = "/((\+|-)([0-9]+\.[0-9]+))((\+|-)[0-9]+)/";
preg_match($regex, $yourstring, $match);
var_dump($match);

this can be done with a regex yes.

 

$subject = "+1.5-165"; //or other way around
$pattern = "~^(\+[0-9]+\.[0-9]+ |-[0-9]+\.[0-9]+)(\+\d+ |-[0-9]+)$~";
preg_match($pattern, $subject, $matches);
foreach($matches as $match){
      print "$match <br />";
}

 

Edit: well I took the time to write it, so i might as well post it! Sorry Gizmo for the redundancy

  Quote

AyKay47, look into a testing tool like RegexCoach.  You can verify your regex's do what you think they do without having to write up a test script.

yeah I was actually talking to xyph a little bit ago about the one that he uses..I will need to look into getting one, will save me some time and produce cleaner regex no doubt...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.