Solved

given height, return inches in int

Posted on 2013-11-16
3
249 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 34

Accepted Solution

by:
gr8gonzo earned 300 total points
Comment Utility
<?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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now