Jump to content

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

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.