I'm trying to identify not only "numbers" in a string, but tag what type of number it is, such as General, Fraction, Percentile, Ratio etc.
Now if I use a tool like http://rubular.com/, my patterns appear to work fine.
Rules?
([-+]?)([0-9]+)([,+]?)([.]?) //General
([-+]?[0-9.,]+[%]) //Percent
([0-9]+[\/][0-9]+(?:st|nd|rd|th)) //Fraction
([-+]?[0-9.,]+[:][-+]?[0-9.,]+) //Ratio
Strings to check?
1
1,000
1.000
-50.5
-1:+3
1,200.6:3.9
+2:-4
1/5th
25%
25.76%
1,001%
It's when I put them in php (if/elseif) constructs I always end up with the "general" number?
if (preg_match('/([-+]?[0-9.,]+[%])/',$string)) {
echo " PERCENTILE ";
} elseif (preg_match('/([0-9]+[\/][0-9]+(?:st|nd|rd|th))/',$string)) {
echo " FRACTION ";
} elseif (preg_match('/([-+]?[0-9.,]+[:][-+]?[0-9.,]+)/',$string)) {
echo " RATIO ";
} elseif (preg_match('/([-+]?)([0-9]+)([,+]?)([.]?)/',$string)) {
echo " CARDINAL ";
} else {
echo " GENERAL ";
}
No matter what, it echos "CARDINAL".
Yes, I've tried switching the order (Card/General first, then the others).
Yes, I've tried making them independant IF()s, and in reverse order (most general first).
Nothing appears to work.
Even a Single IF checking for any of the formats simply fails.
Either my rules are stuffed - or I'm doing something blatantly wrong.
You error must come from somewhere else, I couldn't reproduce it. Try to put your code in an otherwise empty file to test it by itself and it should work fine.
Test.php:
$nums = array(
'1',
'1,000',
'1.000',
'-50.5',
'-1:+3',
'1,200.6:3.9',
'+2:-4',
'1/5th',
'25%',
'25.76%',
'1,001%'
);
foreach ($nums as $string) {
echo $string.': ';
if (preg_match('/([-+]?[0-9.,]+[%])/',$string)) {
echo " PERCENTILE ";
} elseif (preg_match('/([0-9]+[\/][0-9]+(?:st|nd|rd|th))/',$string)) {
echo " FRACTION ";
} elseif (preg_match('/([-+]?[0-9.,]+[:][-+]?[0-9.,]+)/',$string)) {
echo " RATIO ";
} elseif (preg_match('/([-+]?)([0-9]+)([,+]?)([.]?)/',$string)) {
echo " CARDINAL ";
} else {
echo " GENERAL ";
}
echo "\n";
}
$ php test.php
1: CARDINAL
1,000: CARDINAL
1.000: CARDINAL
-50.5: CARDINAL
-1:+3: RATIO
1,200.6:3.9: RATIO
+2:-4: RATIO
1/5th: FRACTION
25%: PERCENTILE
25.76%: PERCENTILE
1,001%: PERCENTILE
Try surrounding each regular expression with ^ and $ so that it matches only if it matches the entire string or line. Without these, if a regular expression matches part of a number, then that's still a match. For example, '1,000' matches your regular expression for CARDINAL numbers, /([-+]?)([0-9]+)([,+]?)([.]?)/, because it matches the substring '1,'. If you add ^ and $, as in /^([-+]?)([0-9]+)([,+]?)([.]?)$/, then it no longer matches.
<?php
function check_format($string) {
if (preg_match('/^([-+]?[0-9.,]+[%])$/',$string)) {
echo " PERCENTILE ";
} elseif (preg_match('/^([0-9]+[\/][0-9]+(?:st|nd|rd|th))$/',$string)) {
echo " FRACTION ";
} elseif (preg_match('/^([-+]?[0-9.,]+[:][-+]?[0-9.,]+)$/',$string)) {
echo " RATIO ";
} elseif (preg_match('/^([-+]?)([0-9]+)([,+]?)([.]?)$/',$string)) {
echo " CARDINAL ";
} else {
echo " GENERAL ";
}
}
array_map("check_format", array(
"1",
"1,000",
"1.000",
"-50.5",
"-1:+3",
"1,200.6:3.9",
"+2:-4",
"1/5th",
"25%",
"25.76%",
"1,001%"
));
outputs:
CARDINAL GENERAL GENERAL GENERAL RATIO RATIO RATIO FRACTION PERCENTILE PERCENTILE PERCENTILE
EDIT: This might be a better regular expression for CARDINAL numbers:
/^([-+]?)([0-9]+)(?:,?[0-9]{3})*([.]?)$/
Please have a look on this .
<?php
$num = 12.20
$num = floatval(preg_replace('/[^\d.]/', '', num));
// output will be 12.2
//it always replace zero and you have number 12.00 then output will be 12
$num = 12.00
$num = floatval(preg_replace('/[^\d.]/', '', num));
// output will be 12
Related
I have an array which has several numbers.
What I want to do is get a loop and run into this array and count how many two-digits numbers, three-digits, four-digits numbers do I have.
I also tried this:
$threeDigits=0;
$fourDigits=0;
$fiveDigits=0;
$sixDigits=0;
$myArray=(123,1234,12345,123456,1234567,111,222)
for($i=1,$size=count($myArray);$i<=$size;i++)
{
if(strlen($==3)) $threeDigits++;
else if(strlen($i==4)) $fourDigits++;
else if(strlen($i==5)) $fiveDigits++;
else if(strlen($i==6)) $sixDigits++;
}
echo "There are " .$fourDigits. " numbers with 4 digits.";
echo "There are " .$threeDigits. " numbers with 3 digits.";
echo "There are " .$fiveDigits. " numbers with 5 digits.";
echo "There are " .$sixDigits. " numbers with 6 digits.";
But somehow it only reads them as one. As you can see, in my array there are three three-digit numbers but when I print it out it says I have only one. What do you think it might be the problem here?
Iteration over an array should be done by using foreach (because in PHP, arrays can have gaps in the offset keys)
foreach($myArray as $i => $myValue) {
// .. do something
}
Secondly, you're determining the string length of $i, which is the key. But you want to use the value at the key:
$myArray[$i]
Considering the example above, $myValue corresponds to $myArray[$i]
Since I couldn't find any answer that "suited" my code better, I tried and solved this out.
Here's my code if someone else in the future needs it.
<?php
$threeDigits=0;
$fourDigits=0;
$fiveDigits=0;
$sixDigits=0;
$myArray=array(123,111,1234,12345,123456);
$size=count($myArray);
foreach($myArray as $el){
if(strlen($el) == 3)
{
$threeDigits++;
}
else if(strlen($el)==4)
{
$fourDigits++;
}
else if(strlen($el)==5)
{
$fiveDigits++;
}
else if (strlen($el)==6)
{
$sixDigits++;
}
}
echo "there are " .$threeDigits. " three digits numbers -- " ;
echo "there are " .$fourDigits. " four digits numbers -- " ;
echo "there are " .$fiveDigits. " five digits numbers -- " ;
echo "there are " .$sixDigits. " six digits numbers -- " ;
?>
This question already has answers here:
PHP Count the lenght of each value in a array/string (tags)
(3 answers)
Closed 1 year ago.
I have an array which has several numbers.
What I want to do is get a loop and run into this array and count how many two-digits numbers, three-digits, four-digits numbers do I have.
What I have tried:
$myArray = array(123,1234,12345,123456,123567);
for($i=1,$size=count($myArray);$i<=$size;i++)
{
if(strlen($i==3)) echo "the number with index " .$i. " is three-digit number.";
else if(strlen($i==4)) echo "the number with index " .$i. " is four-digit number.";
...
}
I also tried this:
$threeDigits=0;
$fourDigits=0;
$fiveDigits=0;
$sixDigits=0;
$myArray=(123,1234,12345,123456,1234567,111,222)
for($i=1,$size=count($myArray);$i<=$size;i++)
{
if(strlen($==3)) $threeDigits++;
else if(strlen($i==4)) $fourDigits++;
else if(strlen($i==5)) $fiveDigits++;
else if(strlen($i==6)) $sixDigits++;
}
echo "There are " .$fourDigits. " numbers with 4 digits.";
echo "There are " .$threeDigits. " numbers with 3 digits.";
echo "There are " .$fiveDigits. " numbers with 5 digits.";
echo "There are " .$sixDigits. " numbers with 6 digits.";
But somehow it only reads them as one. As you can see, in my array there are three three-digit numbers but when I print it out it says I have only one. What do you think it might be the problem here?
Just for the hell of it, map it as a callback:
foreach(array_map('strlen', $myArray) as $key => $len) {
echo "the number with index $key is a $len digit number.";
}
Store the lengths in an array:
$myArray = array(123,1234,12345,123456,123567);
$lengths = [];
foreach ( $myArray as $val ) {
$lengths[strlen($val)]++;
}
foreach (range(2,4) as $len) {
echo "There are " . $lengths[$len] . " $len-digit numbers\n";
}
I have issue in php preg_match..
I tried using preg_match to check this string..
$txt = 'dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.catdog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog.cat.dog';
echo count(explode('.', $txt)) . "<br>";
echo strlen($txt) , "<br>";
if(preg_match("/[a-z]+(?:([.][a-z]+)*)/i", $txt)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
But I think there is limit in preg_match.. the total count after I explode the dot is 1638, more than 1638, it returns not match.
But when I tried using phpliveregex or regex 101 it can match the regex..
so here is the complete explanation..
I created a program for checking a format..
continue from this issue Javascript string match specific regex
User can input anything in textarea, as long as it's correct then it will retrive the data, but when user input wrong format, I should remove the wrong format by using preg_replace and retrive a correct format, when none of format is correct, then it will return error message..
this not 1 format only, there are about 10 formats I should check..
so this is not just a simple to explode the ., +, * or use cytpe_alpha().
The conclusion is the program or the product owner does not care what user input into the text area, they can input 10 formats into the textarea.. as long as I can check and passed the format, then it all be good..
example of wrong input that I should fix into correct format..
150-50-30----20=50+dog......cat.......cow.....chicken,,,,.,.,pencil,
dog,,,.,cat.,.,.chicken......50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+*4*8=32
so after I correct the format, it will be like this..
150-50-30-20=50+dog.cat.cow.chicken.pencil.dog.cat.chicken.50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+4*8=32
can anyone help with this issue?
It appears that you have reached the memory limit on "capturing". If you simply don't capture any substrings, you'll be just fine.
Code: (Demo)
$txt = 'dog.cat' . str_repeat('.dog.cat', 815);
for ($i = 0; $i < 5; ++$i) {
$txt .= '.dog.cat';
echo count(explode('.', $txt)) . "\n";
echo strlen($txt) . "\n";
if (preg_match("/[a-z]+(?:\.[a-z]+)*/i", $txt, $match)) {
echo "A match was found." . strlen($match[0]);
} else {
echo "A match was not found.";
}
echo "\n---\n";
}
Output:
1634
6535
A match was found.6535
---
1636
6543
A match was found.6543
---
1638
6551
A match was found.6551
---
1640
6559
A match was found.6559
---
1642
6567
A match was found.6567
---
About the error: https://3v4l.org/RjfiN#v7.2.0
You are getting error constant 6.
PREG_JIT_STACKLIMIT_ERROR
preg_last_error_msg() isn't available until PHP8.
I see some good answers at: PHP PREG_JIT_STACKLIMIT_ERROR - inefficient regex
If preg_match() is proving to be unsuitable/miserable for your requirements, then stop using it. There are other available tools to determine if a string is strictly comprised of dot-separated alphabetical substrings.
function isDotSeparatedAlphabetical($string) {
foreach (explode('.', $string) as $word) {
if (!ctype_alpha($word)) {
return false;
}
}
return true;
}
$txt = 'dog.cat' . str_repeat('.dog.cat', 815);
for ($i = 0; $i < 5; ++$i) {
$txt .= '.dog.cat';
var_export(['length' => strlen($txt), 'verdict' => isDotSeparatedAlphabetical($txt)]);
echo "\n---\n";
}
I am trying to create a binary/hexadecimal converter to convert a denary(base 10) number/value into binary and hexadecimal.
It works fine so far for binary until the input from the form is greater than 11 digits and over(string length), ruffly as it seems to variety. after 11 digits it starts adding " - " into the outcome. Im not sure were this is coming from as I don't have an " - " in the code.
I wasn't sure if this was something to do with large integers as I saw some other questions on that topic(not in php however it was java, so not sure if there is something simpler in php)
That said I was under the impression that form inputs were always strings.
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). - from : http://www.php.net/manual/en/function.is-float.php
(haven't yet got to hexadecimal but needed to mention it as some of the following code contains parts for it.)
here is the php code (note: I do check user input just not added it yet)
$inpu = $_POST['number'];
$numinput = $_POST['number'];
if (is_numeric($numinput))
{
while ($numinput >= 1)
{
$binary .= $numinput % 2;
$numinput = $numinput / 2;
}
$mult = strlen($binary) % 4;
echo gettype($numinput) . "<br />";
echo gettype($binary) . "<br />";
echo gettype($mult) . "<br />";
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
$revbinary = strrev($binary);
echo $inpu . " in binary = " . $revbinary ;
echo "<br /> <br />";
echo chunk_split($revbinary, 4);
echo "<br />" . gettype($revbinary) . "<br />";
echo gettype($inpu) . "<br />";
}
else
{
if (is_numeric($numinput))
{
echo "$numinput is over the max value of 255";
}
else
{
echo "your entry is not a vaild number <br />";
echo $numinput;
}
}
Im not looking for completed version of this code as you would ruin my fun, I am just wondering why there is a "-" being entered after 11 digits or so. It also did't add the symbol before I added :
$mult = strlen($binary) % 4;
echo $mult . "<br />";
while ($mult < 4)
{
$binary.= "0";
$mult++;
}
This was to split the binary into 4s ( 0011 1101 0010 0110 ).
Edit: wondered if this would be useful:
echo gettype($numinput); result double
echo gettype($binary); result string
echo gettype($mult); result integer
gettype($revbinary); result string
echo gettype($inpu); result string
still trying to work this out myself.
Any advice is much appreciated Thanks
I would suggest simply using decbin() and dechex(). Those are functions included in php, which do exactly what you're trying to accomplish.
You might want to check if it is a number first (like you are already doing).
Then cast it to an integer (through intval()) and then apply decbin() and dechex()
http://php.net/manual/de/function.decbin.php
http://www.php.net/manual/de/function.dechex.php
http://php.net/manual/de/function.intval.php
On a website I have a number of small PHP scripts to automate changes to the text of the site, depending on a figure that's calculated from a MySQL database. The site is for a fundraising group, and the text in question on the home page gives the total amount raised.
The amount raised is pulled from the database and rounded to the nearest thousand. This is the PHP I use to round the figure and find the last three digits of the total:
$query4 = mysql_query("SELECT SUM(amountraised) AS full_total FROM fundraisingtotal;");
$result4 = mysql_fetch_array($query4);
$fulltotal = $result4["full_total"];
$num = $fulltotal + 30000;
$ftotalr = round($num,-3);
$roundnum = round($num);
$string = $roundnum;
$length = strlen($string);
$characters = 3;
$start = $length - $characters;
$string = substr($string , $start ,$characters);
$figure = $string;
(£30,000 is the amount that had been raised by the previous fundraising team from when the project first started, which is why I've added 30000 to $fulltotal for the $num variable)
Currently the text reads:
the bookstall and other fundraising events have raised more than £<? echo number_format($ftotalr); ?>
I've just realised though that because the PHP is rounding to the nearest thousand, if the total's for example £39,200 and it's rounded to £40,000, to say it's more than £40,000 is incorrect, and in that case I'd need it to say 'almost £40,000' or something similar. I obviously need to replace the 'more than' with a variable.
Obviously I need to test whether the last three digits of the total are nearer to 0 or 1000, so that if the total was for example £39,2000, the text would read 'just over', if it was between £39,250 and £39,400 something like 'over', between £39,400 and £39,700 something like 'well over', and between £39,700 and £39,999, 'almost.'
I've managed to get the last three digits of the total as a variable, and I think I need some sort of an if/else/elseif code block (not sure if that would be the right approach, or whether to use case/break), and obviously I'm going to have to check whether the figure meets each of the criteria, but I can't figure out how to do that. Could anyone suggest what would be the best way to do this please?
Instead of nested if else blocks, you could have a function that takes in the dollar amount and inside the function throw the variable in a switch case statement and at a particular case return the correct echo'd text.
Switch cases provide readability over several if else statements. If you need coIde examples, let me know.
Update:
Performing comparisons on integers only, switch case is the best bet. When comparing conditions that result in a boolean value, using if else is what you need.
I had took your code and switched the case to be if else and here is what it looks like with an example output:
function qualifier($figure) {
if ($figure < 249) return "just over";
elseif ($figure < 399) return "over";
elseif ($figure < 699) return "well over";
elseif ($figure < 999) return "almost";
else return "No number entered";
}
echo "We are " . qualifier(250) . " our goal!";
// outputs: "We are over our goal!"
The simplest and most elegant solution imho would be, like Lizard pointed out, to floor the value and always say "over". You could even make exception for the first-1000 problem and floor anything below that to the next 100 or so, something like...
<?
function getFlooredText($amount) {
if($amount < 1000) {
return floor($amount/100)*100;
} else {
return floor($amount/1000)*1000;
}
}
echo "We've raised over £" . getFlooredText(455) . "\n"; // 400
echo "We've raised over £" . getFlooredText(1455) . "\n"; // 1000
echo "We've raised over £" . getFlooredText(38800) . "\n"; // 38000
?>
If you absolutely don't want that you could always do a four or five layer if-sandwich, it would be the easiest approach towards the idea you described but maybe not the prettiest. Something like this, if I understood where you were going with it...
<?
function getRoundedText($amount) {
$n = substr($amount, -3);
if($n < 250) {
return "just over £" . round($amount, -3);
} elseif($n < 500) {
return "well over £" . round($amount, -3);
} elseif($n < 750) {
return "close to £" . round($amount, -3);
} elseif($n < 1000) {
return "almost £" . round($amount, -3);
}
}
echo "We've raised " . getRoundedText(38200) . "\n";
echo "We've raised " . getRoundedText(38400) . "\n";
echo "We've raised " . getRoundedText(38700) . "\n";
echo "We've raised " . getRoundedText(38900) . "\n";
?>
Hope that helps.
You might not need to change the text... take a look at the floor function http://php.net/manual/en/function.floor.php
It will always round down, so you can still say 'more than'.
I would use "approximately" to cover all cases!