I am generating a dictionary in PHP where key and values are added to the dictionary.
But I am not able to fetch the values back. I am using following code:
//some code here
while (($line = fgets($fileServices)) !== false) {
//echo $line.'....................';
if (strpos($line,'header') == false){
$serviceName=explode(",", $line)[0];
$RestartData=explode(",", $line)[1];
$StatusData=explode(",", $line)[2];
$serviceRestartMappingdict[$totalServices]= $serviceName.':'.$RestartData;
$serviceStatusMappingdict[$totalServices]= $serviceName.'_'.$StatusData;
$totalServices = $totalServices+1;
}
}
$counter=0;
//echo $serviceStatusMappingdict[0];
fclose($fileServices);
$counter=0;
for ($i = 0; $i < count($serviceStatusMappingdict); ++$i){
echo '<<<<<<<<<<<<<<<<<<<<<<<'.$serviceStatusMappingdict[$i].'>>>>>>>>>>>>>>>>>>>>>>>>>>>';
}
If I do an echo like echo $serviceStatusMappingdict[0];, I get the value but when I use a loop to access the data I do not get any value.
[EDIT] The problem is coming because of the '<' character. Get rid of them and it will work straight away
To answer the following comments that have appeared, the characters '<' and '>' in combination in html refer to an opening and closure of a tag. ex: <div>
The problem comes because the browser is trying intrepreting it as an unknow element and does not know what to do with it. If you inspect the html code of the page you'll be able to see that the information is actually there, just not properly rendered.
[EDIT] Following Umpert parial answer, I tried this and it does the expected behavior. Can we have more informations on why it does not work in your case :
<?php
$array = array( '0' => "kakfa_ps -ef | grep kafka | grep server.properties",
'1' => "zookeeper_ps -ef | grep zookeeper | grep zookeeper.properties",
'2' => "schemaregistry_ps -ef | grep schema-registry | grep java",
'3' => "influx_/sbin/service influxdb status | grep active | grep running",
'4' => "mysql_/sbin/service mysql status | grep active | grep running",
'5' => "cassandra_/sbin/service cassandra status | grep active | grep exited",
'6' => "aerospike_/sbin/service aerospike status | grep active | grep running");
for($i=0;$i<count($array);++$i){
echo '<<<<<<<<<<<<<<<<<<<<<<<'.$array[$i].'>>>>>>>>>>>>>>>>>>>>>>>>>>>';
}
?>
Same amount of '<' and '>' as in your OP. Just to make sure.
Related
I have this bash file which generates key and expire time :
#!/bin/bash
get_customer_url() {
local IP=${1:-127.0.0.1}
local SECRET=${2:-VERY_COOL_SECRET}
local EXPIRES="$(date -d "today + 30 minutes" +%s)";
local token="$(echo -n "${EXPIRES} VERY_COOL_SECRET" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)"
echo "/${token}/${EXPIRES}/"
}
get_customer_url
and i call it via php like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?>
it works fine , it generates something like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
my problem is i want to put another php result in the some line like this
<?php $output = shell_exec('sh generate-keys-hls.sh'); echo "$output";?><?php echo get_post_meta($post->ID, 'file', true)?>
currently its printing the result in two lines i need it to be one just one line instead of this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/
file.mp4.m3u8
i want it to be like this
/G8INKLc3VfDMYtQT2NTU-w/1530222035/file.mp4.m3u8
without white spaces or multiple lines !
Remove the whitespace around the result of shell_exec().
echo trim($output);
I have an ini file which looks like this:
[236a4e392b6dd0a8409bb91c664ab6468be32555]
76561197961658420=DaRoL
76561197962180350=Spow
76561197962376928=Kolma
[efd3dd758092ad90e35fb634a203c41b90da6333]
76561197964385070=Kelininkas
76561199641652847=Kelininkas
How is it possible in PHP to return the section name by searching with a key name (or optional key value).
e.g. 76561197964385070 -> efd3dd758092ad90e35fb634a203c41b90da6333 and optional
Kelininkas -> efd3dd758092ad90e35fb634a203c41b90da6333
I imported the ini file into an array and I'm able to find the key and item.
But not the key name of the superordinate key.
<?php
header('Content-type: text/plain');
$ini_array = (parse_ini_file("BannedHWs.ini",true));
$steamid="76561197962180330";
function find($item, $key)
{
global $steamid;
if ($key == $steamid)
echo "$key FOUND $item\n";
}
array_walk_recursive($ini_array, 'find');
echo "\n";
print_r ($ini_array); // SHOW ARRAY
?>
Result:
76561197962180330 FOUND Spow
Array
(
[236a4e392b6dd0a8409bb91c664ab6468be32d15] => Array
(
[76561197961658460] => DaRoL
[76561197962180330] => Spow
[76561197962376938] => Kolma
)
[efd3dd758092ad90e35fb634a203c41b90da6895] => Array
(
[76561197964385060] => Kelininkas
[76561199641652827] => Kelininkas
)
)
Thanks in advance
If you are on a linux machine, this might help you. This is not a solution, but a path where you can go further.
$ cat some_config_ini.txt
[efd3dd758092ad90e35fb634a203c41b90da6333]
76561197964385070=Kelininkas
76561199641652847=Kelininkas
$ cat some_config_ini.txt | grep Kelininkas -B 1 | head -1
[efd3dd758092ad90e35fb634a203c41b90da6333]
Above commands will not work with your original file.
$ cat som_config_ini.txt | grep Spow -B 1 | head -1
76561197961658420=DaRoL
However if you know know the key&values under a block, you can try this.
$ cat som_config_ini.txt | grep Spow -B 2 | head -1
[236a4e392b6dd0a8409bb91c664ab6468be32555]
$ cat som_config_ini.txt | grep Kolma -B 3 | head -1
[236a4e392b6dd0a8409bb91c664ab6468be32555]
Be aware of the arguments passed to grep [-B 1/2/3].
I can offer ruby&python solutions if you prefer.
Found my own solution:
<?php
header('Content-type: text/plain');
$ini_array = (parse_ini_file("BannedHWs.ini",true));
$steamid="76561199641652827";
function findID(array $array, $path = null) {
global $steamid;
foreach ($array as $k => $v) {
if (!is_array($v)) {
if ($k == $steamid){
echo "HWID: $path\n";
}
}
else {
findID($v, $path.$k);
}
}
}
findID($ini_array);
//echo "\n";
//print_r ($ini_array); // SHOW ARRAY
?>
Based on:
get parent array name after array_walk_recursive function
Please bear with me since I'm still really new in PHP. So I have a config file like this:
profile 'axisssh2'
server '110.251.223.161'
source_update 'http://myweb.com:81/profile'
file_config 'udp.group-1194-exp11nov.ovpn'
use_config 'yes'
ssh_account 'sgdo.ssh'
I want to create a PHP variable named $currentprofile with the value of axisssh2, the value keeps changing. With grep in bash I can just do
currentprofile=$(cat config | grep ^profile | awk -F "'" '{print $2}')
But I have no idea how to do that with PHP. Please kindy help me how to do that, thank you.
UPDATE:
So I tried preg_match like this but it only shows the value of 1
$config=file_get_contents('/root/config');
$currentprofile=preg_match('/^profile /', $config);
echo "Current Profile: ".$currentprofile;
Please tell me what's wrong.
I'm going out on a limb to answer a question that you didn't ask. You'd be better off using parse_ini_string() or fgetcsv(). The .ini file would need the following format profile='axisssh2', so replace the space:
$array = parse_ini_string(str_replace(' ', '=', file_get_contents($file)));
print_r($array);
Yields:
Array
(
[profile] => axisssh2
[server] => 110.251.223.161
[source_update] => http://myweb.com:81/profile
[file_config] => udp.group-1194-exp11nov.ovpn
[use_config] => yes
[ssh_account] => sgdo.ssh
)
So just:
echo $array['profile'];
But the answer to your question would be:
preg_grep()
preg_match()
preg_match returns the number of matches (which is why you get 1) but you can get the actual matches with a capture group which will populate the third argument:
$config = file_get_contents('/root/config');
$currentprofile = preg_match("/^profile '(.*)'/", $config, $matches);
echo $matches[1];
Thought a lot but could not find a suitable title ... so also suggest on title.
Also I am not showing HTML code to avoid making post larger. With PHP variables names and part of figure shown, its easy to understand the situation.
Actual problem is described here -
Here is a code snippet which is just a small part of a large code. The confusion is b/w two situations. Both are mentioned below. -
PHP Code :
while($npacket>=1) {
$count=0;
$out_file=fopen("packet.txt","w");
while($count<$payload_length ) {
$c=fgetc($file);
fputs($out_file,$c);
$count++;
}
$data_file=`cat packet.txt`;
if($datatyp=="ascii")
$fpayload="-f packet.txt";
else if($datatyp=="hex")
$fpayload="-d0x$data_file";
$random="";
if( $ip=="tcp" || $ip=="udp" )
exec("sendip -v -p ipv6 $fpayload $random $adv_cont -6s $sip $optext -p $proto $cont -$s $sapp_port -$d $dapp_port $dip ");
else {
Problem is Here ....
echo "this is $fpayload it";
exec(" echo sendip -v -p ipv6 $fpayload $random $adv_cont -6s $sip $optext -p icmp $icmp_cont $dip > sample ");
}
usleep("$igap");
if(!$flag)
$npacket--;
}
fclose($out_file);
fclose($file);
}
For 1 packet and payload specified as a input,
1) Input : 616263646566
Output :
$ cat payload.txt
616263646566
$ cat packet.txt
616263646566
$ cat sample
$
2) Input : 6162636465666768
Output :
$ cat payload.txt
6162636465666768
$ cat packet.txt
6162636465666768
$ cat sample
sendip -v -p ipv6 -d0x6162636465666768 -6s 2001::100 -p icmp 2001::200
Please help me find out where does exactly problem lies ?
EDIT:
Permission of files :
$ ls -l payload.txt packet.txt sample
-rwxrwxrwx 1 udit udit 13 Nov 17 20:57 packet.txt
-rwxrwxrwx 1 udit udit 13 Nov 17 20:57 payload.txt
-rwxrwxrwx 1 udit udit 0 Nov 17 20:57 sample
Problem was actually presence of whitespaces and null characters at the end of $fpayload variable. Just needed to add one regular expression syntax in else part.
.........
else if($datatyp=="hex") {
$fpayload="-d0x$data_file";
$fpayload=preg_replace('/\s+/', '', $fpayload);
}
....
I'm really sorry if this is too basic, but I really don't know how to do this.
I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/
EDIT: This is the jquery code i use for the autocomplete:
$(function() {
var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
minLength: 2,
queryRemote: true,
remote: {url: 'autocomplete2.php'}
}}});
The plugin uses a PHP for autocomplete, this is an example, it returns this output: "id, text, null (html I don't need), some html"
$response = array();
$names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'Etc');
// make sure they're sorted alphabetically, for binary search tests
sort($names);
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
foreach ($names as $i => $name)
{
if (!preg_match("/^$search/i", $name)) continue;
$filename = str_replace(' ', '', strtolower($name));
$response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
}
header('Content-type: application/json');
echo json_encode($response);
I need a similar PHP to process this results: http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=
...being "beatles" the $search value, and getting this output:
guid,"name",null,"name<span>n:type name</span>"
So, the first result would be:
0,"The Beatles",null,"The Beatles<span>Band</span>"
Of course I would need to query freebase.com from that PHP. I mean:
+---------------+ +-----------+ +------------+
| | | | | |
| TextboxList +-------->| PHP +------->| Freebase |
| | | | | |
+---------------+ +-----------+ +------+-----+
|
JSON JSON |
TextboxList <--------+ freebase <----------+
Is this possible? Thanks!
Try this:
$response = array();
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
$myJSON = file_get_contents('http://www.freebase.com/private/suggest?prefix=' . urlencode($search));
$musicObj = json_decode($myJSON); // Need to get $myJSON from somewhere like file_get_contents()
foreach ($musicObj->result as $item)
{
$response[] = array($item->guid, $item->name, null, $item->name . '<span>'.$item->{'n:type'}->name.'</span>');
}
header('Content-type: application/json');
echo json_encode($response);
The first JSON-escaped result then gives:
["#9202a8c04000641f800000000003ac10","The Beatles",null,"The Beatles<span>Band<\/span>"]
But despite all this, you really don't need to use PHP at all to do this. You can do this all from JavaScript and avoid an extra trip to your server. If you supply the callback argument to freebase, it can create JSONP (which is JSON wrapped in a call to a function, using a function name of your choice) which you can obtain in jQuery and then manipulate further in JavaScript to your liking. But the above is per your original approach in using PHP.