dlf1987 Posted January 11, 2008 Share Posted January 11, 2008 I have a database that lists out different dimensions of the products i sell, and i need to turn this... 24 15/16 x 20 1/2 x 39 into this... 24 <sup>15/16</sup> x 20 <sup>1/2</sup> x 39 using php... So another words i need to put <sup> around the fraction in each dimensions. Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/ Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 I have a database that lists out different dimensions of the products i sell, and i need to turn this... 24 15/16 x 20 1/2 x 39 into this... 24 <sup>15/16</sup> x 20 <sup>1/2</sup> x 39 using php... So another words i need to put <sup> around the fraction in each dimensions. $string = "24" . "<sup>" . "15/16" . "</sup>" . " x 20" . "<sup>" . "1/2" . "</sup>" . " x 39"; More of your code would be helpful, to determine the variables you would replace each of your example values with. Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436504 Share on other sites More sharing options...
GingerRobot Posted January 11, 2008 Share Posted January 11, 2008 Hows about: <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+/[0-9]+)|','<sup>$1</sup>',$str); echo $str; ?> That will replace any measurements given as a fraction with the same fraction in superscript Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436511 Share on other sites More sharing options...
dlf1987 Posted January 11, 2008 Author Share Posted January 11, 2008 Hows about: <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+/[0-9]+)|','<sup>$1</sup>',$str); echo $str; ?> That will replace any measurements given as a fraction with the same fraction in superscript Sweet that worked perfectly. Oops forgot... Is there a way to remove the space between the fraction and the number before it? Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436521 Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 Before he replies with some regex, I'm going to quickly say trim(); Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436523 Share on other sites More sharing options...
Daukan Posted January 11, 2008 Share Posted January 11, 2008 Here is a non regex way <?php $parts = explode(' ', '24 15/16 x 20 1/2 x 39'); $res = ''; foreach($parts as $k=>$v) { if(strstr($v, '/') ) $res .= ' <sup>'.$v.'</sup> '; else $res .= $v.' '; } echo $res; ?> Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436525 Share on other sites More sharing options...
dlf1987 Posted January 11, 2008 Author Share Posted January 11, 2008 i just want to remove the space between the fraction and the number before it. Like this... 2415/16 x 201/2 x 39 Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436530 Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 i just want to remove the space between the fraction and the number before it. Like this... 2415/16 x 201/2 x 39 <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+/[0-9]+)|','<sup>$1</sup>',$str); $array = explode(" </sup>", $str); $x = 0; $y = 0; $str = ""; while ($x <= sizeof($array)) { $array2 = explode(" ", $array[$x]); while ($y <= sizeof($array2)) { if ($array2[$y] == "x") $str .= " " . $array2[$y] . " " ; else $str .= $array2[$y]; $y++; } $x++; } echo $str; ?> Sure, its a little long. But it does the job. Edit: Something obscure about the first array. Didn't notice when I tested it, but now I can really see. Hmm.. I'll fix it. No wonder it was so long. Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436532 Share on other sites More sharing options...
GingerRobot Posted January 11, 2008 Share Posted January 11, 2008 This seems an awful lot more simple to me: <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+) ([0-9]+/[0-9]+)|','$1<sup>$2</sup>',$str); echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436538 Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 This seems an awful lot more simple to me: <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+) ([0-9]+/[0-9]+)|','$1<sup>$2</sup>',$str); echo $str; ?> I WILL figure out what was wrong with my initial array. Don't close it yet.. please.. Nice short answer though. Extremely simple. =3 HAR HAR! <?php $str = '24 15/16 x 20 1/2 x 39'; $str = preg_replace('|([0-9]+/[0-9]+)|','<sup>$1</sup>',$str); $x = 0; $array = explode(" ", $str); $str = ""; while ($x <= sizeof($array)) { if ($array[$x] == "x") $str .= " " . $array[$x] . " " ; else $str .= $array[$x]; $x++; } echo $str; ?> Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436542 Share on other sites More sharing options...
dlf1987 Posted January 11, 2008 Author Share Posted January 11, 2008 Thanks guys for all you help! Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436545 Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 Thanks guys for all you help! No problem. I personally enjoyed competing with the regex guy. *cough*. Sure, I used his regex initially.. but.. *GAH*. I feel so cheated. *writes new script* Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436547 Share on other sites More sharing options...
The Little Guy Posted January 11, 2008 Share Posted January 11, 2008 Not the most dynamic way, but it doesn't use regex <?php $math = "24 15/16 x 20 1/2 x 39"; list($f, $s, $t, $fo, $fi, $si, $se) = explode(" ",$math); echo $f .' <sup>'.$s.'</sup> '.$t.' '.$fo.' <sup>'.$fi.'</sup> '.$si.' '.$se; ?> Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436548 Share on other sites More sharing options...
twostars Posted January 11, 2008 Share Posted January 11, 2008 Not the most dynamic way, but it doesn't use regex <?php $math = "24 15/16 x 20 1/2 x 39"; list($f, $s, $t, $fo, $fi, $si, $se) = explode(" ",$math); echo $f .' <sup>'.$s.'</sup> '.$t.' '.$fo.' <sup>'.$fi.'</sup> '.$si.' '.$se; ?> Okay, I hate you. Link to comment https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.