₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,124 members, 8,420,477 topics. Date: Thursday, 04 June 2026 at 09:14 PM

Toggle theme

Help Construct An Algorithm In Php Code - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHelp Construct An Algorithm In Php Code (1487 Views)

1 Reply (Go Down)

Help Construct An Algorithm In Php Code by Irelokeh(op): 12:53pm On Aug 13, 2015
Please guys I am building this imei check tool in php syntax for a project,but I need a syntax or algorithm function that will separate every 2nd character in a string
eg

56876578

separates => 6758

thanks
Re: Help Construct An Algorithm In Php Code by jacob05(m): 1:24pm On Aug 13, 2015


$rest = substr("56876578", 0, -4); //5687
$rest = substr("56876578", -4); //6758
Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 3:56pm On Aug 13, 2015
Thanks bro but this echoes 6578 I kind off need a 67598 array
Re: Help Construct An Algorithm In Php Code by jacob05(m): 4:27pm On Aug 13, 2015
Irelokeh:
Thanks bro but this echoes 6578 I kind off need a 67598 array
ok.



$part1 = substr("56876578", 0, -4); //5687
$part2 = substr("56876578", -4); //6758

$array = str_split($part1 ); // [5,6,8,7]
$array1 = str_split($part2 ); // [6,7,5,8]

Or

$array = str_split(substr("56876578", 0, -4)); // [5,6,8,7]
$array1 = str_split(substr("56876578", -4)); // [6,7,5,8]

Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 4:44pm On Aug 13, 2015
jacob05:
ok.

$part1 = substr("56876578", 0, -4); //5687 $part2 = substr("56876578", -4); //6758
$array = str_split($part1 ); // [5,6,8,7] $array1 = str_split($part2 ); // [6,7,5,8]
Or
$array = str_split(substr("56876578", 0, -4)); // [5,6,8,7] $array1 = str_split(substr("56876578", -4)); // [6,7,5,8]
Thanks mahn
Re: Help Construct An Algorithm In Php Code by jacob05(m): 5:24pm On Aug 13, 2015
You're welcome
Re: Help Construct An Algorithm In Php Code by Kidstell: 9:20pm On Aug 13, 2015
function imeiOdd ($imei){
$array=str_split ($imei);
for($i=0; $i <strlen($imei); $i++){
if ($i% 2){
unset ($array [$i]);
}
}
//return implode ($ array);
return array_values ($ array);
}

function imeiEven ($imei){
$array=str_split ($imei);
for($i=0; $i <strlen($imei); $i++){
if (! ($i% 2)){
unset ($array [$i]);
}
}
//return implode ($ array);
return array_values ($ array);
}


$imei='56876578';
$oddNoPositions=imeiOdd ($ imei);
//5867
$evenNoPositions=imeiEven ($ imei);
//6758

Just as u might expect there are easier but more confusing ways of doing this. like "array_walk" function.
if you want these functions to return string instead of numbered arrays then you should remove the comments.
Re: Help Construct An Algorithm In Php Code by jacob05(m): 9:50pm On Aug 13, 2015
funny enough... i didn't read the whole question before posting my solution... grin grin

@Kidstell nice one
Re: Help Construct An Algorithm In Php Code by Nobody: 7:12am On Aug 14, 2015
Kidstell:
function imeiOdd ($imei){
$array=str_split ($imei);
for($i=0; $i <strlen($imei); $i++){
if ($i% 2){
unset ($array [$i]);
}
}
//return implode ($ array);
return array_values ($ array);
}

function imeiEven ($imei){
$array=str_split ($imei);
for($i=0; $i <strlen($imei); $i++){
if (! ($i% 2)){
unset ($array [$i]);
}
}
//return implode ($ array);
return array_values ($ array);
}


$imei='56876578';
$oddNoPositions=imeiOdd ($ imei);
//5867
$evenNoPositions=imeiEven ($ imei);
//6758

Just as u might expect there are easier but more confusing ways of doing this. like "array_walk" function.
if you want these functions to return string instead of numbered arrays then you should remove the comments.
Excellent!
Re: Help Construct An Algorithm In Php Code by babatope88(m):
tongue shocked
Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 12:18pm On Aug 14, 2015
babatope88:
OP try this Version more faster but little confusing.

<?php
$string = '56876578';

function imei($string)
{
$imei = '';
$len = 0.5 * strlen($string) + 1;
for ($i = 1; $i < $len; $i++) {
$j = $i << 1;
$imei .= $string[($j-1)];
}
return $imei;

}

print imei($string); //6758

Thanks guys you guys are awesome,nicee
Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 12:19pm On Aug 14, 2015
Thanks alot guys
Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 12:57pm On Aug 14, 2015
Lols thanks once again guys Here's the algorithm I used earlier (#don't blame me I'm learning!),it works fine and even adds the new array but I'd wanted to see much faster and better code whch I got,Kudos

<?php
$imei = ("4747883257"wink;
$var1 =substr($vic,1,-cool;
echo("$var1"wink;
$var2= substr($vic,3,-6);
echo("$var2"wink;
$var3 = substr($vic,5,-4);
echo("$var3"wink;
$var4 = substr($vic,7,-2);
echo("$var4"wink;
$var5 = substr($vic,9);77827
echo("$var5 "wink;
$eqn = $var1+$var2+$var3+$var4+$var5;
echo ("$eqn1"wink; // echos 77827
?>
Re: Help Construct An Algorithm In Php Code by babatope88(m): 2:03pm On Aug 14, 2015
Irelokeh Have clean up the previous code I post, it was posted through phone.

<?php
$string = '56876578';
function imei($string)
{
$imei = '';
$len = 0.5 * strlen($string);
for ($i = 1; $i <= $len; $i++) {
$j = $i << 1;
$imei .= $string[($j-1)];
}
return $imei;

}

print imei($string);//6758

Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 2:33pm On Aug 14, 2015
Thanks bro
Re: Help Construct An Algorithm In Php Code by DonSegmond(m): 2:51pm On Aug 14, 2015
$imei = implode(array_filter(str_split($string), function ($k) { return $k%2; }, ARRAY_FILTER_USE_KEY));

You can use the following, If you have up to date php at least 5.6.0
Re: Help Construct An Algorithm In Php Code by Nobody: 4:51pm On Aug 14, 2015
DonSegmond:
$imei = implode(array_filter(str_split($string), function ($k) { return $k%2; }, ARRAY_FILTER_USE_KEY));

You can use the following, If you have up to date php at least 5.6.0
Outstanding!
Re: Help Construct An Algorithm In Php Code by jacob05(m): 7:06pm On Aug 14, 2015
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-08-14 19:04:28
Server : localhost@?
PHP version : 5.5.12
Platform : WINNT
--------------------------------------
test_Kidstell_method : 0.016 sec.
test_babatope88_method : 0.002 sec.
--------------------------------------
Total time: : 0.018 sec.

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-08-14 19:04:28
Server : localhost@?
PHP version : 5.5.12
Platform : WINNT
--------------------------------------
test_Kidstell_method : 0.017 sec.
test_babatope88_method : 0.005 sec.
--------------------------------------
Total time: : 0.022 sec.
--------------------------------------------

Unable to test DonSegmond's Method because of my php version (requires 5.6.0)..
Re: Help Construct An Algorithm In Php Code by Irelokeh(op): 9:41am On Aug 15, 2015
jacob05:
--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-08-14 19:04:28
Server : localhost@?
PHP version : 5.5.12
Platform : WINNT
--------------------------------------
test_Kidstell_method : 0.016 sec.
test_babatope88_method : 0.002 sec.
--------------------------------------
Total time: : 0.018 sec.

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-08-14 19:04:28
Server : localhost@?
PHP version : 5.5.12
Platform : WINNT
--------------------------------------
test_Kidstell_method : 0.017 sec.
test_babatope88_method : 0.005 sec.
--------------------------------------
Total time: : 0.022 sec.
--------------------------------------------

Unable to test DonSegmond's Method because of my php version (requires 5.6.0)..
Please help me test my method


$imei = ("4747883257" ;
$var1 =substr($vic,1,- ;
echo("$var1" ;
$var2= substr($vic,3,-6);
echo("$var2" ;
$var3 = substr($vic,5,-4);
echo("$var3" ;
$var4 = substr($vic,7,-2);
echo("$var4" ;
$var5 = substr($vic,9);77827
echo("$var5 " ;
$eqn =
$var1+$var2+$var3+$var4+$var5;
echo ("$eqn1" ; // echos 77827
1 Reply

Is It Compulsory To Write An Algorithm Before CodingImplement The Quick Sort Algorithm In Your Language.My Php Code Does Not Execute But Displays Code On Browser,234

Telegram Bot ChallengeNeed Help On PHPHelp Revolved Error In Android Program