Solved

# given height, return inches in int

Posted on 2013-11-16
Medium Priority
261 Views
given one parameter at a time
6'0"
5'10"
5'10"
5'8"

want an object oriented method to return inches
72
70
70
68
0
Question by:rgb192

LVL 36

Accepted Solution

gr8gonzo earned 1200 total points
ID: 39653937
<?php

\$originalValue = "5'10\"";
echo "{\$originalValue} = " . Converter::ToInches(\$originalValue) . " total inches!\n";

class Converter
{
public static function ToInches(\$original)
{
if(preg_match("/^(\d+)'(\d+)\"/",\$original,\$matches))
{
\$feet = intval(\$matches[1]);
\$inches = intval(\$matches[2]);
return (\$feet * 12) + \$inches;
}
else
{
throw new Exception("I don't understand the value {\$original}!");
}
}
}

?>
0

LVL 111

Assisted Solution

Ray Paseur earned 800 total points
ID: 39654479
You can do it all on one line!

``````<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28296225.html

\$height = <<<EOD
5'8"
EOD;

// TURN OFF STRICT ERROR REPORTING
error_reporting(~E_STRICT);

// COMPUTE HEIGHT IN INCHES
\$inches = current(explode("'", \$height)) * 12 + end(explode("'", rtrim(trim(\$height), '"')));

// SHOW THE WORK PRODUCT
var_dump(\$height, \$inches);
``````
I'm going to church to watch reruns of the Auburn game.  Best to all, over and out, ~Ray
0

LVL 1

Author Closing Comment

ID: 39656640
Thank you.

Even though the second can be done in one line, I like the first more because it teaches me about object oriented programming.

I would like to know more about why/how the first code sample works.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28297075.html
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.