Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help with function

    I am using this function which works fine for a single number entered by user in format 1000 and 1,100

    Code:
    function metersToSquareFeet($meters, $echo = true)
    {
        $m = $meters;
        $valInFeet = $m*10.7639;
        $valFeet = (int)$valInFeet;
        if($echo == true)
        {
            echo $valFeet;
        } else {
            return $valFeet;
        }
    }
    I need to modify for the case where users enter a range of numbers in the format: 1000-2000

    The function should be smart enough to convert both numbers and in this case output in this format 10,763-21,527

    any help would be much appreciated.

    many thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,444
    Thanks
    13
    Thanked 361 Times in 357 Posts
    use preg_replace_callback() with your function on that string. or split and trim the string on the range sign, convert and join together.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thank you Dormilich.. I also saw that i could use the explode() function for this operation..

    Could you help me out with the code i need to split strings "1000 - 2,000" "1,000 -2,000" "1000-2,000" to simply "1000" and "2000" ?

    I am sure this can be done with one clever piece of code only but i wouldn't know how to do this myself.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,444
    Thanks
    13
    Thanked 361 Times in 357 Posts
    explode on - and trim afterwards. if you want to retain spaces, you’ll need a RegExp.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •