I have the following integers
7
77
0
20
in an array. I use them to check from where a call originated.
Numbers like 730010123, 772930013, 20391938.
What I need to do is I need a way to check if the number starts with 7 or 77 for an example.
Is there any way to do this in PHP and avoid a thousand if statements?
One issue I am having is that if I check if the number starts with 7 the numbers that start with 77 are being called as well. Note that 7 numbers are mobile and 77 are shared cost numbers and not equal in any way so I need to separate them.
if (substr($str, 0, 1) == '7') ||{
if (substr($str, 0, 2) == '77'){
//starts with '77'
} else {
//starts with '7'
}
}
I made a little example with a demo array, I hope you can use it:
$array = array(
7 => 'Other',
70 => 'Fryslan!',
20 => 'New York',
21 => 'Dublin',
23 => 'Amsterdam',
);
$number = 70010123;
$place = null;
foreach($array as $possibleMatch => $value) {
if (preg_match('/^' . (string)$possibleMatch . '/', (string)$number))
$place = $value;
}
echo $place;
The answer in this case is "Fryslan". You have to remember that 7 also matches in this case? So you may want to add some metric system in case of two matches.
Is this you want?
<?php
$myarray = array(730010123, 772930013, 20391938);
foreach($myarray as $value){
if(substr($value, 0, 2) == "77"){
echo "Starting With 77: <br/>";
echo $value;
echo "<br>";
}
if((substr($value, 0, 1) == "7")&&(substr($value, 0, 2) != "77")){
echo "Starting With 7: <br/>";
echo $value;
echo "<br>";
}
}
?>
You could use preg_match and array_filter for that
function check_digit($var) {
return preg_match("/^(7|77|0)\d+$/");
}
$array_to_be_check = array("730010123" , "772930013", "20391938");
print_r(array_filter($array_to_be_check, "check_digit"));
A way to do this is to handle the "integer" you received as a number as being a string.
Doing so by something like this:
$number = 772939913;
$filter = array (
'77' => 'type1',
'20' => 'type2',
'7' => 'type3',
'0' => 'type4');
$match = null;
foreach ($filter as $key => $val){
$comp = substr($number, 0, strlen($key));
if ($comp == $key){
$match = $key;
break;
}
}
if ($match !== null){
echo 'the type is: ' . $filter[$match];
//you can proceed with your task
}
Related
I have a string which looks like this:
15-02-01-0000
15-02-02-0000
15-02-03-0000
15-02-04-0000
15-02-05-0000
15-02-10-0000
15-02-10-9100
15-02-10-9101
15-15-81-0000
15-15-81-0024
So the expected output would be:
All account grouping separated by "-" dashes for example: 15-02-01-0000 there is 3 grouping
start with 15
start with 15-02
start with 15-02-01
So the expected output would be:
First it will show
15 --> All account start with "15"
15-02 --> All account start with "15-02"
15-02-01 -- All accounts start with "15-02-01"
15-02-01-0000
15-02-02 -- All accounts start with 15-02-02
15-02-02-0000
15-02-03 -- onwards like above
15-02-03-0000
15-02-04
15-02-04-0000
15-02-05
15-02-05-0000
15-02-10
15-02-10-0000
15-02-10-9100
15-02-10-9101
15-15
15-15-81
15-15-81-0000
15-15-81-0024
I tried to use substr:
$res = substr("15-15-81-0024",3,2);
if ($res == "15") {
} else if ($res < 10 && $res != 00) {
} else {
}
But not working to put grouping.
Could you please suggest any good way?
You can break each data by - and build the array in as much as needed. Notice the use of & in the code as using reference to result array.
Example:
$str = "15-02-01-0000,15-02-02-0000,15-02-03-0000,15-02-04-0000,15-02-05-0000,15-02-10-0000,15-02-10-9100,15-02-10-9101,15-15-81-0000,15-15-81-0024";
$arr = explode(",", $str);
$res = [];
foreach($arr as $e) { // for each line in your data
$a = explode("-", $e); //break to prefix
$current = &$res;
while(count($a) > 1) { // create the array to that specific place if needed
$key = array_shift($a); // take the first key
if (!isset($current[$key])) // if the path not exist yet create empty array
$current[$key] = array();
$current = &$current[$key];
}
$current[] = $e; // found the right path so add the element
}
The full result will be in $res.
I'd probably do something along the lines of:
Could be more efficient if more time was spent on it.
<?php
$random = '15-02-01-0000
15-02-02-0000
15-02-03-0000
15-02-04-0000
15-02-05-0000
15-02-10-0000
15-02-10-9100
15-02-10-9101
15-15-81-0000
15-15-81-0024';
$lines = explode(PHP_EOL, $random);
$accounts = return_count($lines);
var_dump($accounts);
function return_count($lines){
$count_accounts = array();
$possibilties = array();
if(is_array($lines) && !empty($lines)){
foreach($lines as $val){
$line = explode('-', $val);
array_push($possibilties, $line[0], $line[0] . '-' . $line[1], $line[0] . '-' . $line[1] . '-' . $line[2]);
}
foreach($possibilties as $pos){
if(!isset($count_accounts[$pos])){ $count_accounts[$pos] = 0;}
if(search_array($pos, $lines)){
$count_accounts[$pos]++;
}
}
}
return $count_accounts;
}
function search_array($string, $array){
$found = 0;
if(is_array($array) && !empty($array)){
foreach($array as $val){
if (strpos($val, $string) !== false) {
$found = 1;
}
}
if($found == 1){
return true;
}else{
return false;
}
}else{
return false;
}
}
?>
Which returns:
array (size=10)
15 => int 10
'15-02' => int 8
'15-02-01' => int 1
'15-02-02' => int 1
'15-02-03' => int 1
'15-02-04' => int 1
'15-02-05' => int 1
'15-02-10' => int 3
'15-15' => int 2
'15-15-81' => int 2
With PHP if you have a string which may or may not have spaces after the dot, such as:
"1. one 2.too 3. free 4. for 5.five "
What function can you use to create an array as follows:
array(1 => "one", 2 => "too", 3 => "free", 4 => "for", 5 => "five")
with the key being the list item number (e.g the array above has no 0)
I presume a regular expression is needed and perhaps use of preg_split or similar? I'm terrible at regular expressions so any help would be greatly appreciated.
What about:
$str = "1. one 2.too 3. free 4. for 5.five ";
$arr = preg_split('/\d+\./', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);
I got a quick hack and it seems to be working fine for me
$string = "1. one 2.too 3. free 4. for 5.five ";
$text_only = preg_replace("/[^A-Z,a-z]/",".",$string);
$num_only = preg_replace("/[^0-9]/",".",$string);
$explode_nums = explode('.',$num_only);
$explode_text = explode('.',$text_only);
foreach($explode_text as $key => $value)
{
if($value !== '' && $value !== ' ')
{
$text_array[] = $value;
}
}
foreach($explode_nums as $key => $value)
{
if($value !== '' && $value !== ' ')
{
$num_array[] = $value;
}
}
foreach($num_array as $key => $value)
{
$new_array[$value] = $text_array[$key];
}
print_r($new_array);
Test it out and let me know if works fine
need help replacing array value
i`m trying check experience and space exists in the below array and replace space with experience max (i,e next array values if its equal null/space replace with experience max),
i have pasted code below which im using to find and replace array value, its replacing the experience to experience max instead of empty to experience max
array[1] => name,
array[2] => Work Experience,
array[3] => ,
array[4] => company,
array[5] => location
o/p :
array[1] => name,
array[2] => Work Experience,
array[3] => Work Experience Max,
array[4] => company,
array[5] => location
$search = "Work Experience";
foreach ($val as $key=> $cnt) {
// echo "inside foreach ".$cnt;
if ($cnt == $search) {
echo $keyvalue = $key;
break;
}
}
if (isset($keyvalue)) {
$firstarryval = array_splice($val, $keyvalue, 1);
}
if ($firstarryval == '') {
array_splice($val, $keyvalue, 0, "Work Experience Max");
} else {
array_splice($val, $keyvalue, 0, "Work Experience Max");
}
Ok, here is the solution explained:
$arr = array( 0=>'name', 1=>'Work Experience', 2=>'', 3=>'company', 4=>'location' );
var_export($arr); echo '<br/>';
$search = "Work Experience";
$arrkey = false;
foreach($arr as $key=>$value){ // set variables and advance internal pointer
echo "inside foreach $key => $value <br/>";
if( $value == $search ) {
echo $key;
echo $arrkey = key($arr); // internal php pointer already points to next array key
break;
}
}
if ($arrkey!=false && empty($arr[$arrkey])) {
$arr[$arrkey] = 'Work Experience Max';
}
echo '<br/>'; var_export($arr);
Useful links: foreach, key, empty. Hope it will help! :-)
In PHP I have a list of country telephone codes. e.g: US 1, Egypt 20 ....
I need to check if a given string starts with 00[ANY NUMBER FROM THE LIST].
How can I achieve this and return the country code?
$codes = array(1 => 'US', 20 => 'Egypt');
$phone = '002087458454';
foreach ($codes as $code => $country) {
if (strpos($phone, "00$code") === 0)
break;
}
echo $code; // 20
echo $country; // Egypt
Referring to PHP - get all keys from a array that start with a certain string
foreach ($array as $key => $value) {
if (substr($value, 0, 2) == "00") {
echo "$key\n";
}
}
Use regular expressions:
$str ="0041";
if(preg_match('#00[0-9]+#', $str, $array)){
echo substr($array[0], 2);
}
Use the substr function:
$country_code = substr($string, 0, 2);
Use explode function to separate the string in arrays, and you can acess with $string.
I've been struggling to find a nice algorithm to change a number (could be a float or integer) into a nicely formated human readable number showing the units as a string. For example:
100500000 -> '100.5 Mil'
200400 -> '200.4 K'
143000000 -> '143 Mil'
52000000000 -> '52 Bil'
etc, you get the idea.
Any pointers?
I'd adapt the code below (which i found on the net):
Code credit goes to this link i found: http://www.phpfront.com/php/human-readable-byte-format/
function humanReadableOctets($octets)
{
$units = array('B', 'kB', 'MB', 'GB', 'TB'); // ...etc
for ($i = 0, $size =$octets; $size>1024; $size=$size/1024)
$i++;
return number_format($size, 2) . ' ' . $units[min($i, count($units) -1 )];
}
Don't forget to change 1024 to 1000 though ...
<?php
function prettyNumber($number) // $number is int / float
{
$orders = Array("", " K", " Mil", " Bil");
$order=0;
while (($number/1000.0) >= 1.5) { // while the next step up would generate a number greater than 1.5
$order++;
$number/=1000.0;
}
if ($order)
return preg_replace("/\.?0+$/", "",
substr(number_format($number, 2),0,5)).$orders[$order];
return $number;
}
$tests = array(100500000,200400,143000000,52000000000);
foreach ($tests as $test)
{
echo $test." -> '".prettyNumber($test)."'\n";
}
Here is a log() version if you are still interested:
function wordify($val, $decimalPlaces = 1) {
if ($val < 1000 && $val > -1000)
return $val;
$a = array( 0 => "", 1 => "K", 2 => "Mil", 3 => "Bil", 4 => "Tril", 5 => "Quad" );
$log1000 = log(abs($val), 1000);
$suffix = $a[$log1000];
return number_format($val / pow(1000, floor($log1000)), $decimalPlaces, '.', '') . " $suffix";
}
$tests = array(-1001, -970, 0, 1, 929, 1637, 17000, 123456, 1000000, 1000000000, 1234567890123);
foreach ($tests as $num) {
echo wordify($num)."<br>";
}
found this
this one might be better for you
might be a good start
there is similar code here:
http://aidanlister.com/2004/04/human-readable-file-sizes/