Jump to content


Photo

Problem with a regex


  • Please log in to reply
2 replies to this topic

#1 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 05 March 2006 - 10:40 AM

Well, its my first time using regex and I'm having a bit of a problem: I need to split the following string:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
a:6:{s:12:"firstinsertd";s:10:"01-01-1970";s:12:"firstinsertt";s:8:"01:00 AM";s:7:"updated";s:10:"10-13-2005";s:7:"updatet";s:8:"05:37 PM";s:6:"amount";D:51218.2087766306431149132549762725830078125;s:10:"updatetime";i:1131550652;}
[/quote]
So I can get the amount (512218) alone, without the decimal, or ;D:... But I just can't figure out how to do it.

Heres my regex, which seems to return the number AFTER the decimal as [1] in the array (Along with ";s:10:"updatetime";i:1131550652;} )", for some reason) :
$ucashbank = 'a:6:{s:12:"firstinsertd";s:10:"01-01-1970";s:12:"firstinsertt";s:8:"01:00 AM";s:7:"updated";s:10:"10-13-2005";s:7:"updatet";s:8:"05:37 PM";s:6:"amount";d:51218.2087766306431149132549762725830078125;s:10:"updatetime";i:1131550652;}';


$test = preg_split("/amount\";[d]:([0-9])*[.]/", $ucashbank);
print_r($test);

Returns:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Array ( [0] => a:6:{s:12:"firstinsertd";s:10:"01-01-1970";s:12:"firstinsertt";s:8:"01:00 AM";s:7:"updated";s:10:"10-13-2005";s:7:"updatet";s:8:"05:37 PM";s:6:" [1] => 2087766306431149132549762725830078125;s:10:"updatetime";i:1131550652;} )
[/quote]

Anyone know what I'm doing wrong?

#2 JasperBosch

JasperBosch
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationApeldoorn, The Netherlands

Posted 05 March 2006 - 11:56 AM

Hi Sykoi,

I think you try the wrong way. Why use regular expression?

Why not use unserialize?

I should do it this way:

$var = unserialize($ucashbank);
$amount = ceil($var['amount']);

This wil return the result you disire.

#3 Nolongerused3921

Nolongerused3921
  • Members
  • PipPipPip
  • Advanced Member
  • 142 posts

Posted 05 March 2006 - 12:01 PM

Wow that worked very well, thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users