Solved

given height, return inches in int

Posted on 2013-11-16
3
251 Views
Last Modified: 2013-11-18
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
Comment
Question by:rgb192
3 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 300 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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);

Open in new window

I'm going to church to watch reruns of the Auburn game.  Best to all, over and out, ~Ray
0
 

Author Closing Comment

by:rgb192
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question