Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:38:29+00:00 2026-05-20T12:38:29+00:00

I assumed there’d be an easy way in PHP to convert a string like

  • 0

I assumed there’d be an easy way in PHP to convert a string like 18 5/16 into the float 18.3125. I can’t find a straightforward function to do it. Is there one, or do I need to write my own?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:38:30+00:00Added an answer on May 20, 2026 at 12:38 pm

    I don’t think such a function exists — at least not bundled with PHP.

    Writing a function that does this operation, if your string always have the same format, should not be too hard ; for example, I’d say that something like this should do the trick :

    $str = '18 5/16';
    var_dump(calc($str));
    
    function calc($str) {
        $int = 0;
        $float = 0;
        $parts = explode(' ', $str);
        if (count($parts) >= 1) {
            $int = $parts[0];
        }
        if (count($parts) >= 2) {
            $float_str = $parts[1];
            list($top, $bottom) = explode('/', $float_str);
            $float = $top / $bottom;
        }
        return $int + $float;
    }
    

    Which will get you the following output :

    float 18.3125
    

    And you might get something shorter with a few regex ; something like this should do the trick, I suppose :

    function calc($str) {
        if (preg_match('#(\d+)\s+(\d+)/(\d+)#', $str, $m)) {
            return $m[1] + $m[2] / $m[3];
        }
        return 0;
    }
    


    Else, not bundled in PHP, but already existing, maybe this class could help : Eval Math.

    Disclaimer : I have not tested it — so not quite sure it’ll work in your specific situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When reading from a IO::Socket::INET filehandle it can not be assumed that there will
I assumed there were only bug fixes/(no new types) in .NET 2.0 SP1 until
I assume there must be a system and language independent way to just stick
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Assume I have 10 Methods and 10 Properties. Is there a way to add
I've found a bunch of iPhone objects inside interface builder, but I assumed there
I have been unable to find a way to anti-alias my rendering on iPhone
I can't seem to set a ContentTemplate for a ComboBoxItem. There reason I'm trying
I have a php script that i run via command line like php file.php
How do I have a script run every, say 30 minutes? I assume there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.