Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am using this function which works fine for a single number entered by user in format 1000 and 1,100
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
You're going to need to parse the input yourself.
If you enter in 1000-2000 as a parameter it will think that it's -1000 as it will do the math.
I'm suggesting you change your input to either 2 parameters, an array or a string.
If using a string, use substring to find the int position of '-' http://us1.php.net/manual/en/function.substr.php or use explode like #crayonviolet suggested
Convert both numbers by your constant (10.7639)
Return your desired output as a string or as part of an array
Your choice depends on how your convertToSquareFeet function is used in other parts of your application and the code rework it would have by changing the parameter list.
Hope that helps!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In php, how do I check if a variable is a string or digit?
I'm doing a count() using mysql, and wanted to know if the output is a string or a digit.
I know about is_numeric() and other functions in php, but is there something, if supplied a value will echo if the value is a digit or string?
Just try with gettype function.
gettype('foo'); // string
gettype(1.23); // double
gettype(155); // integer
The problem is if you use a framework or a data abstraction layer, and this treat the output of the database, customarily data type is lost along the way and everything becomes string.
is_numeric is the most indicated in this cases!!!
Short answer:
Use:
echo $gettype($YourValueFromDB);
and in your situation it will output:
string
OR
if(is_int($YourValueFromDB))
{
echo 'we have an integer type!';
}
elseif(is_string($YourValueFromDB))
{
echo 'we have a string type!';
}
else
{
echo 'Not sure: '.gettype($YourValueFromDB);
}
Long answer:
MySQL produces a number value with this query:
select count(*) as total from table
// can range from 0 - infinity (or exhausted memory limit, whichever comes first)
// for this post, pretend this count() returns a 9
When this data is passed back to PHP it is automatically converted into a string due to PHP's extremely relaxed type-casting.
Running this:
var_dump($row);
would produce something like this:
array
'total' => string '9' (length=1)
indicating that you are working with a literal string but it's value is a number so things like if($row['total'] > 1){ } would go inside the curly braces without a problem.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if(preg_match('/^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/', '10-30-2013')){
echo 'true';
}
else {
echo 'false';
}
This not give me true. I think I'm wrong with regex. please tell how to correct this regex
I suggest not using regex at all for this -- date validation with regex is a surprisingly difficult thing to get right, due to the variations possible in the number of days in any given month.
Far better to simply use PHP's DateTime class to actually parse it into a date object. This will also validate it; if it doesn't meet the specified format, then it won't parse.
$dateObj = DateTime::CreateFromFormat('m-d-Y',$inputString);
See PHP manual page for CreateFromFormat().
You should use dedicate functions to parse date ie.:
if (strptime ('10-30-2013' , 'm-d-Y') !== false) {
echo 'true'.PHP_EOL;
} else {
echo 'true'.PHP_EOL;
}
Actually as the others confirmed, your regex works fine and returns true.
But as you made your code shrunken, I think the input string you're trying to show us isn't exactly just a date and it's within a string or maybe has trailing spaces!
So using ^ and $ at your regex will result in failure.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I tried to use php rtrim() function to retrive username in the email address, but I met the following problem:
Case 1:
$email = 'merkerxu37#stackoverflow.com';
echo rtrim($email, '#stackoverflow.com');
I got the output:
merkerxu37
Case 2:
$email = 'merkerxu37#37signals.com';
echo rtrim($email, '#37signals.com');
I got the output:
merkerxu
Can anybody tell me why the "37" is missing in Case 2?
That is because you have entered the second parameter '#37signals.com' which consider 3, 7 or 37 as characters to be trimmed.
Reference
trim (and its single-sided variants) take a list of characters to remove. Since 3 and 7 feature in the list you gave, it trimmed them.
Why not just do this?
echo explode("#",$email,2)[0];
// If you don't have PHP 5.4:
// $parts = explode("#",$email,2); echo $parts[0];