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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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(); Quote Link to comment 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; ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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; ?> Quote Link to comment 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; ?> Quote Link to comment Share on other sites More sharing options...
dlf1987 Posted January 11, 2008 Author Share Posted January 11, 2008 Thanks guys for all you help! Quote Link to comment 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* Quote Link to comment 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; ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.