Parse ini file using PHP with symbol - php

Hi I need to parse the ini file shown below using php, which is basically created using Qt QSettings, I have tried the code here, but seems the INI file are different format there.
Is it possible to parse INI file in below format using PHP ?
[General]
Address=
Email=test#gmail.com
Password=123$
Sync=0
Loggedin=1
[Drives]
1\drive=/
1\free=281
1\size=908
2\drive=/media/New_Volume
2\free=6
2\size=7
3\drive=/media/sdb2
3\free=906
3\size=916
size=2
[Cam]
1\detection=0
1\Area.height=2.4
1\Area.width=9.5
2\detection=0
2\Area.height=2.4
2\Area.width=9.5
3\detection=0
3\Area.height=2.4
3\Area.width=9.5
size=2
Update:
Actually the above ini was working as said in below answer, and my actyual ini contains the $ symbol, that was creating the problem.
[General]
Address=
Email=test#gmail.com
Password=123$
Sync=0
Loggedin=1
[Drives]
1\drive=/
1\free=281
1\size=908
2\drive=/media/New_Volume
2\free=6
2\size=7
3\drive=/media/sdb2
3\free=906
3\size=916
size=2
[Cam]
1\detection=0
1\Area.height=2.4
1\Area.width=9.5
2\detection=0
2\Area.height=2.4
2\Area.width=9.5
3\detection=0
3\Area.height=2.4
3\Area.width=9.5
size=2
And seems I have to escape the $ symbol.

It works just fine, take a look at the php code while the .ini file stays exactly as you have it.
$ini_array = parse_ini_file("settings.ini",true);
print_r($ini_array);
//will output
Array
(
[General] => Array
(
[Address] =>
[Email] => test#gmail.com
[Password] => 123$ // note the $ sign in value here
[Sync] => 0
[Loggedin] => 1
)
[Drives] => Array
(
[1\drive] => /
[1\free] => 281
[1\size] => 908
[2\drive] => /media/New_Volume
[2\free] => 6
[2\size] => 7
[3\drive] => /media/sdb2
[3\free] => 906
[3\size] => 916
[size] => 2
)
[Cam] => Array
(
[1\detection] => 0
[1\Area.height] => 2.4
[1\Area.width] => 9.5
[2\detection] => 0
[2\Area.height] => 2.4
[2\Area.width] => 9.5
[3\detection] => 0
[3\Area.height] => 2.4
[3\Area.width] => 9.5
[size] => 2
)
)

Related

PHP opcache preload is not loaded on Apache

I'm using Apache/2.4.29 with PHP 7.4.5 as module.
Apache and PHP are configured with lito as user.
Virtualhost configuration with PHP settings:
php_admin_value opcache.enabled 1
php_admin_value opcache.preload /home/lito/www/preload.php
php_admin_value opcache.preload_user lito
phpinfo show opcache configuration:
opcache.enable On
opcache.preload /home/lito/www/preload.php
opcache.preload_user lito
No errors on apache log.
After apache restart opcache_get_status haven't any key related with preload status (preload_statistics) and there are only one script preloaded (current phpinfo.php):
Array
(
[opcache_enabled] => 1
[cache_full] =>
[restart_pending] =>
[restart_in_progress] =>
[memory_usage] => Array
(
[used_memory] => 9168872
[free_memory] => 125047784
[wasted_memory] => 1072
[current_wasted_percentage] => 0.00079870223999023
)
[interned_strings_usage] => Array
(
[buffer_size] => 6291008
[used_memory] => 522888
[free_memory] => 5768120
[number_of_strings] => 10969
)
[opcache_statistics] => Array
(
[num_cached_scripts] => 1
[num_cached_keys] => 1
[max_cached_keys] => 16229
[hits] => 1
[start_time] => 1587378881
[last_restart_time] => 0
[oom_restarts] => 0
[hash_restarts] => 0
[manual_restarts] => 0
[misses] => 3
[blacklist_misses] => 0
[blacklist_miss_ratio] => 0
[opcache_hit_rate] => 25
)
[scripts] => Array
(
[/home/lito/www/phpinfo.php] => Array
(
[full_path] => /home/lito/www/phpinfo.php
[hits] => 0
[memory_consumption] => 1040
[last_used] => Mon Apr 20 12:39:15 2020
[last_used_timestamp] => 1587379155
[timestamp] => 1587378955
)
)
)
Is not available opcache preload on Apache with PHP as module (without a custom php.ini)?
Thanks!
UPDATE: Tested adding preload file on /etc/php/7.4/apache2/php.ini and it works fine.
Similar (for nginx with php-fpm): https://bugs.php.net/bug.php?id=79043#1578412872
As far as I can tell, it looks like preloading is not in effect when configured this way.
Answer from PHP core dev:
yes, as preloading happens during early server startup, enabling it through php_admin_value does not work.

Post array incomplete when receiving a post

I'm on a Drupal7 site, and I'm not used to Drupal. When I edit a node (standard page), and try to save it, the menu disappears. Not all node's are like this, only the ones that uses a field group of heatmaps, probably a custom field group (legacy).
System specs are:
CentOS 6.6
Apache 2.2
Mysql 5.5
Php 7
At first, I thought it was a bug in Drupal 7, and I tried solutions as Menu items disappearing in Drupal 7. But the suggested solutions didn't work. So I started to suspect post_max_size or memory_limit, because the form with the custom field grows very large when it uses the field or the field group. So I've maxed the memory settings and it looks good but is still not working.
The field group array tends to be very large and I tried to find some info about nestling level too big in a post but found no hints.
The post size is:
post_max_size in bytes = 536870912
post CONTENT_LENGTH = 1020347
The field group contains Heatmaps with Geolocations and endless of data:
[field_heatmap_data] => Array
(
[und] => Array
(
[0] => Array
(
[tablefield] => Array
(
[cell_0_0] => X
[cell_0_1] => Y
[cell_0_2] => Plastic
[cell_0_3] => Paper
[cell_0_4] => Glass
[cell_0_5] => Metal
[cell_0_6] => Organiskt
[cell_0_7] =>
[cell_0_8] =>
[cell_0_9] => Other
[cell_1_0] => 14.1741233638
[cell_1_1] => 57.7797089972
[cell_1_2] => 0
[cell_1_3] => 0
[cell_1_4] =>
[cell_1_5] =>
[cell_1_6] =>
[cell_1_7] => 1
[cell_1_8] =>
[cell_1_9] => 2
[cell_2_0] => 14.1784435935
[cell_2_1] => 57.7797106709
[cell_2_2] => 0
[cell_2_3] => 0
[cell_2_4] =>
[cell_2_5] =>
[cell_2_6] =>
[cell_2_7] =>
[cell_2_8] =>
[cell_2_9] =>
[cell_3_0] => 14.1656472109
[cell_3_1] => 57.7831198751
[cell_3_2] => 1
[cell_3_3] => 2
[cell_3_4] => 1
[cell_3_5] => 1
[cell_3_6] =>
[cell_3_7] =>
[cell_3_8] =>
[cell_3_9] =>
[cell_4_0] => 14.1753179083
[cell_4_1] => 57.7826699822
[cell_4_2] => 0
[cell_4_3] => 5
[cell_4_4] =>
[cell_4_5] => 3
[cell_4_6] =>
[cell_4_7] => 9
[cell_4_8] => 4
[cell_4_9] =>
[cell_5_0] => 14.1602465906
[cell_5_1] => 57.7824661754
[cell_5_2] => 2
[cell_5_3] => 0
[cell_5_4] => 1
[cell_5_5] =>
[cell_5_6] =>
[cell_5_7] => 4
[cell_5_8] =>
[cell_5_9] => 1
[cell_6_0] => 14.1552312791
[cell_6_1] => 57.7788985858
[cell_6_2] => 0
[cell_6_3] => 1
[cell_6_4] =>
[cell_6_5] => 1
[cell_6_6] =>
[cell_6_7] => 4
[cell_6_8] =>
[cell_6_9] =>
[cell_7_0] => 14.1631063952
[cell_7_1] => 57.7813178687
[cell_7_2] => 1
[cell_7_3] => 0
[cell_7_4] =>
[cell_7_5] =>
[cell_7_6] =>
[cell_7_7] => 2
[cell_7_8] => 3
[cell_7_9] =>
[cell_8_0] => 14.1742044644
[cell_8_1] => 57.7827544419
[cell_8_2] => 0
[cell_8_3] => 0
[cell_8_4] =>
[cell_8_5] =>
[cell_8_6] =>
[cell_8_7] => 4
[cell_8_8] => 1
[cell_8_9] =>
[cell_9_0] => 14.157952438
[cell_9_1] => 57.7818974962
[cell_9_2] => 2
[cell_9_3] => 4
[cell_9_4] => 5
[cell_9_5] => 1
[cell_9_6] =>
[cell_9_7] => 8
[cell_9_8] => 2
[cell_9_9] =>
[cell_10_0] => 14.1706946744
[cell_10_1] => 57.7815507326
[cell_10_2] => 0
[cell_10_3] => 0
[cell_10_4] =>
And so on....
So I've figured out that there is a flaw in the architecture of the node because it cant clearly not handle that much data in a field group and should been handled as an separate node, but since this is a legacy project I don't want to mess things up.
If I var_dump the $_POST variable on different pages when editing, I can clearly see that the $_POST variable stops after the $_POST['field_heatmap'] element where there is data, while the pages that doesn't contain data in that field group the $_POST array continues after the $_POST['field_heatmap'] element.
So my question is, should I continue to try to find a bug in Drupal or should I investigate further some php settings (Or maybe apache). I've tried debugging with cachegrind but can't find any unusual. Or any hints are greatly appreciated!
Finally! The max_input_vars was set to 1000
Changed it to max_input_vars = 10000 and it worked!

PHP locale with money_format not working with fr_CA

I have this simple code:
<?php
setlocale('LC_MONETARY', 'fr_CA.UTF-8');
echo money_format('%+#10n', '-4562.75834');
print_r(localeconv());
?>
Who normally should give -4 562,76 $. But the result is always this:
(4 562,76 $)
The output of localeconv():
Array
(
[decimal_point] => .
[thousands_sep] =>
[int_curr_symbol] => CAD
[currency_symbol] => $
[mon_decimal_point] => ,
[mon_thousands_sep] =>
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 0
[p_sep_by_space] => 1
[n_cs_precedes] => 0
[n_sep_by_space] => 1
[p_sign_posn] => 1
[n_sign_posn] => 0
[grouping] => Array
(
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
We can see that negative sign should be - but not ()
The locale on the server (which is Debian 7), is installed:
# locale-gen
Generating locales (this might take a while)...
en_CA.UTF-8... done
fr_CA.ISO-8859-1... done
fr_CA.UTF-8... done
Generation complete.
I have tried with PHP 5.4.45 and 5.6.17
You have put the LC_MONETARY between single quotes.
It needs to live as a const variable which is set as not in quotes.
setlocale(LC_MONETARY, 'fr_CA.UTF-8');

Undefined Index for Valid key from array in PHP

While parsing a CSV file(I have used a Class found on Google), I ran into a problem. Here is an example of an array made from a .csv file(print_r):
Array
(
[0] => Array
(
[Site] => ViralNova
[Impressions] => 104719
[CTR] => 0.30%
[Clicks] => 311
[Average CPC] => $ 0.400
[CPM] => $ 1.19
[Conversion Rate] => 1.29%
[Actions] => 4
[CPA] => $ 31.100
[Spent] => $ 124.40
)
[1] => Array
(
[Site] => TMZ - Desktop
[Impressions] => 103276
[CTR] => 0.29%
[Clicks] => 295
[Average CPC] => $ 0.400
[CPM] => $ 1.14
[Conversion Rate] => 0.68%
[Actions] => 2
[CPA] => $ 59.000
[Spent] => $ 118.00
)
)
The Problem is that I cannot use the "Site" index. Whenever I try to, I get this notice:
<b>Notice</b>: Undefined index: Site in <b>C:\xampp\htdocs\panel\update\assets\php\core\core-ajax.php</b> on line <b>104</b><br />
Line 104 being:
print $data[0]['Site'];
Also,
print $data[0]['Impressions'];
or
print_r($data[0]);
works without any problem and I get the correct value/array.
Any ideas?
Thanks!!
Acc. to me your code is running fine.
this problem is occur when in loop in a point your index is missing.
to resolve this issue..
use this..
<?php
if($data[0]['Site']){
print $data[0]['Site'];
}
// Also
if($data[0]['Impressions']){
print print $data[0]['Impressions'];
}
?>

Extract item from an array

I want to be able to extract the output of [HostName] from this array in PHP. Thank you!
Array
(
[Protocol] => 17
[HostName] => [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
[Map] => ba_jail_hellsgamers_fx6
[ModDir] => cstrike
[ModDesc] => Counter-Strike: Source
[AppID] => 240
[Players] => 12
[MaxPlayers] => 64
[Bots] => 0
[Dedicated] => d
[Os] => l
[Password] =>
[Secure] => 1
[Version] => 1.0.0.75
[GamePort] => 27015
[ServerID] => -682722125
[GameTags] => HLstatsX:CE,alltalk,bunnyhopping,cool,drugs,free,gameme,gang,hellsgamers,hg,increased_maxplayers,jail,jailbr,nostats,startmoney
)
Like this:
echo $Array[HostName]; // output: [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
where $Array is the array you printed and copied here.
I recommend you start reading stuff from php.net - as this is the most basic thing you can do.
Thats the most simplest question .........
Just use this
echo $Your_array[HostName];

Categories