I am trying to display numbers retrieved from the database, in a specific format in a text box.
There are two ways in which the numbers can be displayed.
When the total numbers are 10
in database (4608061019) Expected output 46-0806-1019
When the total numbers are 13
in database (4608061019100) Expected output 46-0806-1019-100
My progress so far:
While saving the value into the database I am using
preg_replace("/[^0-9]/","",$string); // to make sure all hardcoded "-" are removed while storing.
One possible (regex-using) approach:
$str = '4608061019';
$formatted = preg_replace(
'/(^\d{2}|\d{4})(?!$)/', '$1-', $str);
// 46-0806-1019
Demo. This function doesn't check the string's length - it just adds a hyphen after each relevant sequence of symbols (2 right after the beginning, 4 afterwards).
Easy! If you are sure that there are only these two options, then you can do this way:
Convert the number to an array of numbers:
$number = str_split($number);
Check the length:
if (count($number) == 10)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9];
else if (count($number) == 13)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9] . "-" . $number[10] . $number[11] . $number[12];
Return the number:
return $number;
The full function here:
function tokenize($number)
{
$number = str_split($number);
if (count($number) == 10)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9];
else if (count($number) == 13)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9] . "-" . $number[10] . $number[11] . $number[12];
return $number;
}
Output
echo tokenize(4608061019);
echo tokenize(4608061019100);
Output
46-0806-1019
46-0806-1019-100
Fiddle: http://codepad.viper-7.com/EVWeFR
Another alternative solution:
$parts = array(2,4,4,3);
$string = "4608061019100";
$i = 0;
$newString = '';
foreach ($parts as $part) {
$newString.=substr($string, $i, $part) ."-";
$i = $i+$part;
}
$newString = rtrim($newString, "-");
Output is:
string '46-0806-1019-100' (length=16)
Works for 46-0806-1019 too.
try it:
//$number = '4608061019';
$number = '4608061019100';
function formatImportantNumber($theNumber){
$formatedNumber = '';
if(strlen($theNumber)==10){
//46-0806-1019
$formatedNumber = substr($theNumber, 0, 2).'-'.substr($theNumber, 2, 4).'-'.substr($theNumber, 6, 4);
}elseif(strlen($theNumber)==13){
//46-0806-1019-100
$formatedNumber = substr($theNumber, 0, 2).'-'.substr($theNumber, 2, 4).'-'.substr($theNumber, 6, 4).'-'.substr($theNumber, 10, 3);
}else{
die('Invalid number formated')
}
return $formatedNumber;
}
echo formatImportantNumber($number);
Related
I created a Text Plane File .txt from Storage with Laravel, but it is always in the first Row, put 2 spaces, I haven't been able to delete it.
I used trim(), ltrim(), replace() etc with php.
Thanks you.
To result of text plain:
$ruta = 'pila/' . $anio . '/' . $mes;
$nombreArchivo = date('Y-m-d H:i:s') . '_' . $datosEmpresa['TIDE'] . '_' . $datosEmpresa['IDEM'] . '_' . $datosEmpresa['OPERADOR'] . '_' . $datosEmpresa['PERIODO'] . '_' . $datosEmpresa['TIPOPLANILLA'] . '_TIPO_2.txt';
$archivo = $ruta . '/' . $nombreArchivo;
$contenido = null;
/* - - - - - - - - - - - - - - - - - *
* REGISTRO TIPO 1 *
* - - - - - - - - - - - - - - - - - */
$campo1 = '01';
$campo2 = str_pad($datosEmpresa['MPLA'], 1);
$campo3 = str_pad('0001', 4);
$campo4 = str_pad($datosEmpresa['RASO'], 200);
$campo5 = str_pad($datosEmpresa['TIDE'], 2);
$campo6 = str_pad($datosEmpresa['IDEM'], 16);
$campo7 = str_pad($datosEmpresa['DIGV'], 1);
$campo8 = str_pad($datosEmpresa['TIPOPLANILLA'], 1);
$campo9 = str_pad($datosEmpresa['PLANILLAANTERIOR'], 10);
$campo10 = str_pad($datosEmpresa['FECHAPLANILLAANT'], 10);
$campo11 = str_pad($datosEmpresa['PRESENTACION'], 1);
$campo12 = str_pad($datosEmpresa['CODSUCURSAL'], 10);
$campo13 = str_pad($datosEmpresa['NOMSUCURSAL'], 40);
$campo14 = str_pad($datosEmpresa['CARL'], 6);
$campo15 = str_pad($datosEmpresa['PERIODO'], 7);
$campo16 = str_pad($datosEmpresa['PERIODOSALUD'], 7);
$campo17 = str_pad(" ", 10); //PLANILLA
$campo18 = str_pad(" ", 10); //FECHA PAGO
$campo19 = str_pad(count($datosTrabajador), 5, '0', STR_PAD_LEFT);
$campo20 = str_pad($datosEmpresa['TOTALNOMINA'], 12, '0', STR_PAD_LEFT);
$campo21 = str_pad($datosEmpresa['TAPO'], 2);
$campo22 = str_pad($datosEmpresa['OPERADOR'], 2);
$contenido .= $campo1 . $campo2 . $campo3 . $campo4 . $campo5 . $campo6 . $campo7 . $campo8 . $campo9 . $campo10 . $campo11 . $campo12 . $campo13 . $campo14 . $campo15 . $campo16 . $campo17 . $campo18 . $campo19 . $campo20 . $campo21 . $campo22;
Storage::append($archivo, $contenido);
I attach my source code:
enter image description here
Only for the first row, the others one it's OK.
So usually the Diffie-Hellman key is 2048 bits as I understand but my computer can barely calculate a 10 digit number. What are some common numbers in Diffie-Hellman?
Here is my code that's suuuper slow:
$gen = 77;
$mod = 517165;
$saltA = 1233217;
$saltB = 5173123;
$calculatedSecretKeyA = gmp_mod(gmp_pow($gen, $saltA), $mod);
$calculatedSecretKeyB = gmp_mod(gmp_pow($gen, $saltB), $mod);
$calcKeyA = gmp_mod(gmp_pow($calculatedSecretKeyB, $saltA), $mod);
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
$calcKeyB = gmp_mod(gmp_pow($calculatedSecretKeyA, $saltB), $mod);
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
Use gmp_powm
gmp_powm ( GMP|int|string $num , GMP|int|string $exponent , GMP|int|string $modulus ) : GMP
for the below lines.
$calculatedSecretKeyA = gmp_powm($gen, $saltA, $mod);
$calculatedSecretKeyB = gmp_powm($gen, $saltB, $mod);
$calcKeyA = gmp_powm($calculatedSecretKeyB, $saltA, $mod);
$calcKeyB = gmp_powm($calculatedSecretKeyA, $saltB, $mod);
It uses the modular form of square-and-multiply technique. The intermediate values will never exceed mod^2. Also, it has O(log n) complexity.
For awhile, I've been trying to make this code work for this form's database, but it just wouldn't work properly.
This is the form input without PI_ID.
This is the form input with PI_ID.
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'] . "-" . $_POST['cr_phase'] . "-" . $_POST['cr_pi_id'];
This output was faulty as the output will be CR-001-08-18- -, if I don’t want to input any module or phase, because the lines behind 18 are not supposed to be there.
Basically, the output is CR-001-08-18-Marketing-PH1-PI1, the Module(Marketing), Phase(PH1), and PI_ID(PI1) is optional to fill so it could either be:
CR-001-08-18-Marketing(Module filled, Phase unfilled, PI_ID unfilled)
CR-001-08-18-PH1(Module unfilled, Phase filled, PI_ID Unfilled)
CR-001-08-18-PI(Module unfilled, Phase unfilled, PI_ID filled)
This was the code I where tried using if-else:
$phase=$_GET['cr_phase'];
$modul=$_GET['cr_modul'];
$crpid=$_GET['cr_pi_id'];
if ($phase!='' && $modul='' && $crpid='')
{
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_phase'];
}else if($phase='' && $modul!='' && $crpid='')
{
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'];
}else if($phase='' && $modul='' && $crpid=!'')
{
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_pi_id'];
}else if($phase!='' && $modul!='' && $crpid!='')
{
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'] . "-" . $_POST['cr_phase'] . "-" . $_POST['cr_pi_id'];
}else
{
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'];
}
But, it doesn't work as it just displays CR-001-08-18 without showing the phase and module, even when I inputted the phase, module, and PI_ID.
Can anyone help?
Only three if condition needed.Also check the method(GET or POST) you are using to submit the form and fetch the data accordingly or use $_REQUEST.
$phase=$_POST['cr_phase'];
$modul=$_POST['cr_modul'];
$crpid=$_POST['cr_pi_id'];
$values = "";
if($phase != '')
{
$values .= "-$phase";
}
if($modul != '')
{
$values .= "-$modul";
}
if($crpid != '')
{
$values .= "-$crpid";
}
$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . $values;
The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
http://php.net/manual/en/control-structures.switch.php
$n = 3;
function derde_macht($n) {
return pow($n, 3);
}
{
echo $n . "x" . $n . "x" . $n . "=" . derde_macht(3);
}
How could I make this so that you can choose a parameter for $n = 3;?
Got this :
derde_macht($n) you need to pass your $n in that function instead of derde_macht(3).
$n = 10;
function derde_macht($n) {
return pow($n, 3);
}
{
echo $n . "x" . $n . "x" . $n . "=" . derde_macht($n);
}
I am trying to update a variable value that is within an array variable value.
You will see I am writing out a file with: file_put_contents(). The implode("\r\n", $contents)... contains the $contents variable.
I need $body_file_count to increment every iteration of the if ($i == $per_file) {...
It's pretty evident the $contents array cannot update a variable value in this case $body_file_count.
$body_file_count is the number of files outputted. It's actually the same number in the file title: $file_count...
Basically, I just need to write the $body_file_count to the:
$default_contents=$contents=array("BODY CONTENT TOP . "$body_file_count" . ");
on each if ($i == $per_file) { iteration. Obviously I could scrap $body_file_count if I could pass $file_count to the $content as $file_count is updating the title as expected.
$body_file_count = 0;
$footer = "FOOTER";
$default_contents = $contents = array("BODY CONTENT TOP . "$body_file_count" . ");
while ($row = mysql_fetch_array($result)) {
$line = "...";
$contents[] = $line; // Each array element will be a line in the text file
$i++;
$recs++;
if ($i == $per_file) {
$contents[] = $footer; // Add the footer to the end
file_put_contents($_POST["a"] . "-#" . $file_count . "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate . '.txt', implode("\r\n", $contents));
$i = 0;
$recs = 0;
$contents = $default_contents;
$file_count++;
$body_file_count++;
} // End of if()
} // End of while()
First beware that you have forget to add the string concatenation operator (".") on the $default_contents initialitation
I don't know if i have understand well your question. If i have understand well your problem you can try to update the the $default_contents everytime that you change $body_file_count++
$body_file_count = 0;
$footer = "FOOTER";
$default_contents = $contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
while ($row = mysql_fetch_array($result)) {
$line = "...";
$contents[] = $line; // Each array element will be a line in the text file
$i++;
$recs++;
if ($i == $per_file) {
$contents[] = $footer; // Add the footer to the end
file_put_contents($_POST["a"] . "-#" . $file_count . "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate . '.txt', implode("\r\n", $contents));
$i = 0;
$recs = 0;
$file_count++;
$body_file_count++;
$default_contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
$contents = $default_contents;
} // End of if()
} // End of while()
Also if you don't need for anything else this variable besides to provide an initial content then you can just take it away
$body_file_count = 0;
$footer = "FOOTER";
$contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
while ($row = mysql_fetch_array($result)) {
$line = "...";
$contents[] = $line; // Each array element will be a line in the text file
$i++;
$recs++;
if ($i == $per_file) {
$contents[] = $footer; // Add the footer to the end
file_put_contents($_POST["a"] . "-#" . $file_count . "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate . '.txt', implode("\r\n", $contents));
$i = 0;
$recs = 0;
$file_count++;
$body_file_count++;
$contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
} // End of if()
} // End of while()