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.
Related
$array = array(
"[ci_id]" => '144309',
"[NEW flag]" => 'No',
"[[*PRODUCT_IMAGE_ANCHOR1*]]" => ,
"[[*PRODUCT_IMAGE2*]]" => '154154154'
);
I need to get the elements which have pattern like '[['.
I have tried by using array_key_exists():-
if (array_key_exists('[*PRODUCT_IMAGE2*]', $array))
But i want to match only with '['
Can any one help me on this
Use preg_grep() with array_keys() like below:-
$matches = preg_grep ('/[.*?]/i', array_keys($array));
print_r($matches);
Output:- https://eval.in/817299
Or can do it using strpos() also:-
foreach($array as $key=>$val){
if(strpos($key,'[')!== false){
echo $key ."is matched with [*] pattern";
echo PHP_EOL;
}
}
Output:- https://eval.in/817297
The following code will give the true for the key with double [[ in if you know the element index value
$test = array("a"=>'a',"[a]"=>'a',"[[a]]"=>'a',"b"=>'b',"c"=>'c');
var_dump(array_key_exists("[[a]]", $test));
If you can checking the keys to determine if [[ even exist then the following code should work
$test = array("a"=>'a',"[a]"=>'a','[[a]]'=>'a',"b"=>'b',"c"=>'c');
$values = array();
foreach ($test as $key=>$value) {
if (stripos('[[', substr($key, 0, 2)) !== false) {
array_push($values, $value);
}
}
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
}
I'd like to get some values out of an array and print them out on the page.
For [1], these things should be extracted: USD 7.0269 6.4119 0.14231 0.15596
The array looks like this:
print_r($arr);
[1] => USD United States of America 7.0269 6.4119 Dollars 0.14231 0.15596 � Copyright 2003-2011. Powered by CurrencyXchanger 3.580
[2] => EUR Euro Member Countries 9.0373 8.3253 Euro 0.1107 0.1201 � Copyright 2003-2011. Powered by CurrencyXchanger 3.580
What is the best solution to accomplish this?
I'd use preg_match_all() after I trim off the area of interest:
foreach ($arr as $line) {
// currency is in the first four characters (apparently)
$currency = substr($line, 0, 4);
// we use everything left of 'Copyright'
$rest = strstr($line, 'Copyright', true);
// match each occurrence of nn.nnnn
if (preg_match_all('/\d+\.\d+/', $rest, $matches)) {
// $matches[0] contains all the amounts
echo $currency, ' ', join(' ', $matches[0]), PHP_EOL;
}
}
For PHP < 5.2 you need this line to calculate $rest:
$rest = substr($line, 0, strpos($line, 'Copyright'));
Demo
Here is a regex solution:
foreach($arr as $key => $item)
{
preg_match('/^([A-Z]){3}[\sA-Za-z]+(\d+\.\d+)\s+(\d+\.\d+)\s+[A-Za-z]+\s+(\d+\.\d+)\s+(\d+\.\d+)/', $item, $matches);
$result[$key] = array_shift($matches);
}
The regex corresponds to your pattern and captures everything you want inside consecutive elements of $matches. Since $matches[0] represents the full match, we remove the first element and assign it to your result array.
Try
foreach($arr as $v) {
$items = explode(' ', $v);
$new_arr[] = $items[0]; //Getting the currency type
foreach($items as $k => $m) {
if(is_numeric($m) && floor($m) != $m && $k != (count($items) - 1))
$new_arr[] = $m;
}
}
//displaying the $new_arr
foreach($new_arr as $n) {
if(is_numeric($n) === FALSE)
echo "\n";
echo $n . ' ';
}
See it in action here
Done quickly:
$result = array_map(
function ($string) {
preg_match_all('/(\d+\.\d+)\s/', $string, $matches);
return substr($string, 0, 3) . ' ' . implode(' ', $matches[1]);
},
$arr
);
Result:
Array
(
[0] => USD 7.0269 6.4119 0.14231 0.15596
[1] => EUR 9.0373 8.3253 0.1107 0.1201
)
With Regular Expressions you can get it.
foreach($arr as $key => $value) {
preg_match_all('/(\d+\.\d+)/', $value, $matches);
$result[substr($value, 0, 3)] = array_shift($matches);
}
You get an Array like this
var_dump($result);
array (
'USD' => array( 7.0269, 6.4119, 0.14231, 0.15596 )
'EUR' => array( 9.0373, 8.3253, 0.1107, 0.1201 )
)
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
I've got an array called $myarray with values like these:
myarray = array (
[0] => eat-breakfast
[1] => have-a-break
[2] => dance-tonight
[3] => sing-a-song
)
My goal is to search for a part of this array and get the rest of it. Here is an example:
If i submit eat, I would like to get breakfast.
If i submit have, I would like to get a-break.
I just try but I'm not sure at all how to do it...
$word = 'eat';
$pattern = '/'.$word.'/i';
foreach ($myarray as $key => $value) {
if(preg_match($pattern, $value, $matches)){
echo $value;
}
}
print_r($matches);
It displays:
eat-breakfastArray ( )
But I want something like that:
breakfast
I think I'm totally wrong, but I don't have any idea how to proceed.
Thanks.
use
stripos($word, $myarray)
<?php
$myarray = array (
'eat-breakfast',
'have-a-break',
'dance-tonight',
'sing-a-song'
) ;
function search($myarray, $word){
foreach($myarray as $index => $value){
if (stripos($value, $word) !== false){
echo str_replace(array($word,'-'), "", $value);
}
}
}
search($myarray, 'dance');
echo "<br />";
search($myarray, 'have-a');
echo "<br />";
search($myarray, 'sing-a');
demo
I think the word you seek is at the beginning. Try this
function f($myarray, $word)
{
$len = strlen($word);
foreach($myarray as $item)
{
if(substr($item, 0, $len) == $word)
return substr($item, $len+1);
}
return false;
}
You're feeding the wrong information into preg_match, although I'd recommend using array_search().. Check out my updated snippet:
$word = 'eat';
$pattern = '/'.$word.'/i';
foreach ($myarray as $key => $value) {
if(preg_match($pattern, $value, $matches)){
echo $value;
}
}
print_r($matches);
To get rid of that last bit, just perform a str_replace operation to replace the word with ""
This will both search the array (with a native function) and return the remainder of the string.
function returnOther($search, $array) {
$found_key = array_search($search, $array);
$new_string = str_replace($search . "-", "", $array[$found_key]);
return $new_string;
}