Jump to content

[SOLVED] help adding <sup> </sup>


Recommended Posts

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

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

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

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

Link to comment
https://forums.phpfreaks.com/topic/85537-solved-help-adding/#findComment-436532
Share on other sites

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

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.