How to check if string contains just 1 same number [duplicate] - php

This question already has answers here:
Php Regular Expression repeated characters
(2 answers)
Closed 9 years ago.
I've got a Call Me Back form which sends me a phone number of a person who wants to be called back. Today I received the form with '88888888' instead of a real phone number.
How can I check if the string contains 1 and the same number, continous?
There must not be more than 4 same numbers in a row.

To check if the string contains only one repeated integer, string -> array, check if the unique count is 1.
<?php
$string = "88888888";
$array = array_unique( str_split( $string ) );
$result = $array;
if( count($result) === 1 ) {
echo "Same number repeated in string";
}else{
echo "More than 1 number found in string";
}
?>
-Edit-
optimized: Removed for loop thanks to comment by #Uberfuzzy

$number_string = (string)$number_string;
return strlen($number_string) > 0 && str_repeat($number_string[0], strlen($number_string)) === $number_string;

Related

How to change all front decimal to 0 in php [duplicate]

This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod

how to set the min and max to 2 decimal places in php [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
<?php
$result = array_filter($_POST);
if(!empty($result)){
echo ((max($result)+min($result))/2)*1.2 ."|".((max($result)+min($result))/2)*1.3;
}
?>
hi all..how to set min and max result to 2 decimals places for the code above? actually the result will appears in input type text after the process complete
Use number_format function in PHP.
number_format ( float $number [, int $decimals = 0 ] ) : string
For more information see here
$yourNumber = 1235.343;
echo number_format ($yourNumber, 2);
// Will output 1,235.34 (with two decimals, specified in number_format as second paramter.
Edit: Max / Min functions return mixed value. Make sure its float and then pass it to number_format. It will returns you the string.

How to show only two numbers after decimal using php eval() or round()? [duplicate]

This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example

Print numbers as words? [duplicate]

This question already has answers here:
PHP: Express Number in Words [duplicate]
(4 answers)
Closed 8 years ago.
Hi I was wondering how can I print out numbers in their word form ? When I did a Google search it showed other peoples script on how to print 234 as two hundred thirty four .
I need 234 as two three four.
Thank you guys!
I need 234 as two three four.
It would be as simple as creating an array of your number to word map, then taking your number and printing out the corresponding value:
<?php
$num_word = array();
$num_word[0] = 'zero';
$num_word[1] = 'one';
$num_word[2] = 'two'
...
$num_word[9] = 'nine';
$num = 234;
foreach(str_split($num) as $w) {
echo $num_word[$w];
}
?>

PHP substring not working for intergers [duplicate]

This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);

Categories