mrbill Posted June 22, 2007 Share Posted June 22, 2007 Been trying to select part of a number (price) contained in a variable. Here is a sample of the code I have been attempting to get to work for this $amount = "$52.99"; $n = sscanf($amount, "%s %s", $dollars, $cents); echo "<h2>" . $dollars . "</h2> <em>" .$cents. "</em>"; The above outputs everything to the $dollars and nothing to $cents. The dollar amount won't always be 2 digits so I would like to be able to just seperate from the right to the decimal, that is *.99* only and the dollar amount from the decimal to the dollar sign, *$52*. All I am trying to do is take the $amount which is in this example $52.99 and separate the dollar from the cents so I can apply a style. It should look something like this: $52.99 Link to comment https://forums.phpfreaks.com/topic/56651-solved-selecting-part-of-a-variable/ Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 echo (int) $number; try that one Link to comment https://forums.phpfreaks.com/topic/56651-solved-selecting-part-of-a-variable/#findComment-279766 Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 or you can try the pregmatch ASTIG!! Link to comment https://forums.phpfreaks.com/topic/56651-solved-selecting-part-of-a-variable/#findComment-279769 Share on other sites More sharing options...
sasa Posted June 22, 2007 Share Posted June 22, 2007 try <?php $amount = "$52.99"; $n = preg_match('/([\$0-9]+)\.([0-9]+)/',$amount,$a); echo "<h2>" . $a[1] . " <sup>" .$a[2]. "</sup></h2>"; ?> Link to comment https://forums.phpfreaks.com/topic/56651-solved-selecting-part-of-a-variable/#findComment-279771 Share on other sites More sharing options...
mrbill Posted June 22, 2007 Author Share Posted June 22, 2007 Bingo! We have a winner. I am going to use the example code from SASA. Thank you Teng84 also for your response. Link to comment https://forums.phpfreaks.com/topic/56651-solved-selecting-part-of-a-variable/#findComment-279777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.