A shipping service is providing me with this weird text format response, after some research i found it is very close to a .net array and i am trying to convert to PHP so i can do something useful with it.
My questions:
What is this format?
Is there any PHP function i could use to parse it?
City[0] = "Elghorashi";
State[0] = "";
ZipCode[0] = "";
CCity[0] = "Elghorashi";
CState[0] = "";
CZipCode[0] = "";
City[1] = "Abugibha";
State[1] = "";
ZipCode[1] = "";
CCity[1] = "Abugibha";
CState[1] = "";
CZipCode[1] = "";
Thanks
Here's a quick little snippit I whipped up to convert a string that looks like a PHP array into an actual PHP array. To be clear the $newArray variable is where the array is created to.
$string = 'City[0] = "Elghorashi"; State[0] = ""; ZipCode[0] = ""; CCity[0] = "Elghorashi"; CState[0] = ""; CZipCode[0] = ""; City[1] = "Abugibha"; State[1] = ""; ZipCode[1] = ""; CCity[1] = "Abugibha"; CState[1] = ""; CZipCode[1] = "";';
$string = substr($string, 0, -1);
$arr = explode('; ', $string);
$newArray = [];
foreach($arr AS $value){
$keyVals = explode(' = ', $value);
preg_match_all('/^(.*?)\[([0-9]+)\]$/', $keyVals[0], $matches);
$value = is_string($keyVals[1]) ? substr($keyVals[1], 1, -1) : $keyVals[1];
$newArray[$matches[2][0]][$matches[1][0]] = $value;
}
Related
I have code like this:
$ares_ico_fin = "";
$ares_dic_fin = "";
$ares_firma_fin = "";
$ares_ulice_fin = "";
$ares_cp1_fin = "";
$ares_cp2_fin = "";
$ares_mesto_fin = "";
$ares_psc_fin = "";
$ares_stav_fin = "";
$file = #file_get_contents("http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=".$_POST["ico_ajax_send"]);
if($file)
{
$xml = #simplexml_load_string($file);
}
if($xml)
{
$ns = $xml->getDocNamespaces();
$data = $xml->children($ns['are']);
$el = $data->children($ns['D'])->VBAS;
if (strval($el->ICO) == $_POST["ico_ajax_send"])
{
$ares_ico_fin = strval($el->ICO);
$ares_dic_fin = strval($el->DIC);
$ares_firma_fin = strval($el->OF);
$ares_ulice_fin = strval($el->AA->NU);
$ares_cp1_fin = strval($el->AA->CD);
$ares_cp2_fin = strval($el->AA->CO);
if($ares_cp2_fin != ""){ $ares_cp_fin = $ares_cp1_fin."/".$ares_cp2_fin; }else{ $ares_cp_fin = $ares_cp1_fin; }
$ares_mesto_fin = strval($el->AA->N);
$ares_psc_fin = strval($el->AA->PSC);
$ares_stav_fin = 1;
}
It works fine on my local machine, but I do not get any data if tries it on remoteserver...
I read help like: Fixing error with file_get_contents permission denied
with no success, thanks for ideas how to solve it, have a nice day subii
Dear Friend I creating Import feature in Codeigniter using below function but I don't know how to escape when duplicated value happen on my data when my loop selected from excel
The Primary key is only used for $in_val['in_no'] and other value is free to insert.
How to escape duplicated value and try to insert other row from excel into database?
Here is my Function
public function imported($file_paths) {
$in_val = array();
$i = 0;
$cell_array = $this->csvimport->get_array($file_paths);
foreach ($cell_array as $row) {
if ($i === 7000)break; {
$in_val['user_id'] = 70;
$in_val['in_no'] = (int) preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$2\n", trim($row['IN_number']));
$in_val['fi_no'] = trim($row['Fir_obtic']);
$in_val['paid_by'] = trim($row['Paid_by']);
$in_val['in_per'] = str_replace('/', '-', trim($row['In_date']));
$in_val['sale_by'] = trim($row['Sale_by']);
$in_val['billed'] = trim($row['Billed']);
$in_val['in_date'] = str_replace('/', '-', trim($row['In_date']));
$in_val['top_up_num'] = trim($row['T_P_number']);
$in_val['ffrom'] = trim(str_replace('/', '-', $row['From']));
$in_val['fto'] = str_replace('/', '-', trim($row['to']));
$in_val['pay_du_date'] = str_replace('/', '-', trim($row['Pay_due_date']));
$in_val['bill_amount'] = trim(str_replace('/', '-', $row['Bill_Amount']));
$in_val['bill_to'] = trim($row['Cust_name']); //customer name
$in_val['receiv_date'] = trim($row['R_C_date']);
$in_val['adds'] = trim($row['Address']);
$in_val['bill_item'] = str_replace('/', '-', trim($row['Bill_Item']));
$in_val['descr'] = trim($row['Description']);
$in_val['in_type'] = strtolower(preg_replace("/(\D+\d{0})0*([0-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
$in_val['quant'] = trim($row['In_Quanity']);
$in_val['unit_p'] = trim($row['In_Unit_P']);
$in_val['amount'] = trim($row['Bill_Amount']);
$in_val['total'] = trim($row['Total']);
$in_val['status'] = 1;
$in_val['amc'] = trim($row['Amount_Description']);
$pri_val['user_id'] = 70;
$pri_val['type'] = strtolower(preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
$pri_val['parent'] = 1;
$pri_val['status'] = 1;
$pri_val['in_no'] = (int) $in_val['in_no'];
$pri_val['total'] = (int) trim($row['Total']);
$pri_val['in_date'] = $in_val['in_date'];
$this->db->insert('dbfinan_invoice', $in_val);
$this->db->insert('prifix', $pri_val);
$i++;
}
}
}
Please help
try add condition and function to check in table like this:
if($this->[yourmodal]->isExist($in_val['in_no'])==false)
{
$this->db->insert('dbfinan_invoice', $in_val);
$this->db->insert('prifix', $pri_val);
}
So $in_val['in_no'] in table can unique
//Chars to be replaced
$suchmuster = array();
$suchmuster[0] = 'á';
$suchmuster[1] = 'à';
$suchmuster[2] = 'â';
$suchmuster[3] = 'é';
$suchmuster[4] = 'è';
$suchmuster[5] = 'ê';
$suchmuster[6] = 'í';
$suchmuster[7] = 'ì';
$suchmuster[8] = 'î';
$suchmuster[9] = 'ó';
$suchmuster[10] = 'ò';
$suchmuster[11] = '/ô/';
$suchmuster[12] = 'ú';
$suchmuster[13] = 'ù';
$suchmuster[14] = 'û';
$suchmuster[15] = ' ';
$suchmuster[16] = '.';
$suchmuster[17] = ',';
$suchmuster[18] = '-';
$suchmuster[19] = '_';
//Replaces
$ersetzungen = array();
$ersetzungen[0] = 'a';
$ersetzungen[1] = 'a';
$ersetzungen[2] = 'a';
$ersetzungen[3] = 'e';
$ersetzungen[4] = 'e';
$ersetzungen[5] = 'e';
$ersetzungen[6] = 'i';
$ersetzungen[7] = 'i';
$ersetzungen[8] = 'i';
$ersetzungen[9] = 'o';
$ersetzungen[10] = 'o';
$ersetzungen[11] = 'o';
$ersetzungen[12] = 'u';
$ersetzungen[13] = 'u';
$ersetzungen[14] = 'u';
$ersetzungen[15] = '';
$ersetzungen[16] = '';
$ersetzungen[17] = '';
$ersetzungen[18] = '';
$ersetzungen[19] = '';
$newmessage = preg_replace($suchmuster, $ersetzungen, strtolower($message));
The above code was created to replace some special chars with normal ones but no matter which $nachricht (= $message) I put in, it always gives back an empty string!
Thanks for your help.
EDIT
I Changed it to:
//Chars to be replaced
$suchmuster[0] = '/ /';
$suchmuster[1] = '/./';
$suchmuster[2] = '/,/';
$suchmuster[3] = '/-/';
$suchmuster[4] = '/_/';
//Replaces
$ersetzungen[0] = '';
$ersetzungen[1] = '';
$ersetzungen[2] = '';
$ersetzungen[3] = '';
$ersetzungen[4] = '';
$newmessage = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', strtolower($message));
$newmessage = preg_replace($suchmuster, $ersetzungen, $newmessage);
It still gives back an empty string.
Instead of using several patterns, you can also combine the pattern to:
$nachrichtneu = preg_replace('/[,.\s_-]/', '', $nachrichtneu);
And strtolower will not convert Á and the like to á. First do the iconv conversion, then the strtolower.
Regarding your updated question:
Regular expressions use special control characters. In your case it is /./ which causes your result to be empty. . means "any character" in regex, so you are replacing each and every character with nothing.
use preg_quote() to escape special characters in regular expressions. Example: $xy = '/' . preg_quote('.') . '/';
But you can do this much simpler without using regular expressions, like:
$replaces = array(
'.' => '',
',' => '',
// ...
);
$message = str_replace(array_keys($replaces), array_values($replaces), $message);
change your code through
$suchmuster = array();
$suchmuster[0] = "/á/";
$suchmuster[1] = "/à/";
...
I came across this php script to pull IceCast stats (listeners,current song) from my streaming server.
It was published here
Use PHP to show Icecast2 statistics
<?php
/*
* SCRIPT CONFIGURATIONS
*/
$SERVER = 'http://myserver.com:8000'; //URL TO YOUR ICECAST SERVER
$STATS_FILE = '/status.xsl'; //PATH TO STATUS.XSL PAGE YOU CAN SEE IN YOUR BROWSER (LEAVE BLANK UNLESS DIFFERENT)
///////////////////// END OF CONFIGURATION --- DO NOT EDIT BELOW THIS LINE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//create a new curl resource
$ch = curl_init();
//set url
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE);
//return as a string
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//$output = our stauts.xsl file
$output = curl_exec($ch);
//close curl resource to free up system resources
curl_close($ch);
//build array to store our radio stats for later use
$radio_info = array();
$radio_info['server'] = $SERVER;
$radio_info['title'] = '';
$radio_info['description'] = '';
$radio_info['content_type'] = '';
$radio_info['mount_start'] = '';
$radio_info['bit_rate'] = '';
$radio_info['listeners'] = '';
$radio_info['most_listeners'] = '';
$radio_info['genre'] = '';
$radio_info['url'] = '';
$radio_info['now_playing'] = array();
$radio_info['now_playing']['artist'] = '';
$radio_info['now_playing']['track'] = '';
//loop through $ouput and sort into our different arrays
$temp_array = array();
$search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>";
$search_td = array('<td class="streamdata">','</td>');
if(preg_match_all("/$search_for/siU",$output,$matches)) {
foreach($matches[0] as $match) {
$to_push = str_replace($search_td,'',$match);
$to_push = trim($to_push);
array_push($temp_array,$to_push);
}
}
//sort our temp array into our ral array
$radio_info['title'] = $temp_array[0];
$radio_info['description'] = $temp_array[1];
$radio_info['content_type'] = $temp_array[2];
$radio_info['mount_start'] = $temp_array[3];
$radio_info['bit_rate'] = $temp_array[4];
$radio_info['listeners'] = $temp_array[5];
$radio_info['most_listeners'] = $temp_array[6];
$radio_info['genre'] = $temp_array[7];
$radio_info['url'] = $temp_array[8];
$x = explode(" - ",$temp_array[9]);
$radio_info['now_playing']['artist'] = $x[0];
$radio_info['now_playing']['track'] = $x[1];
?>
Dose someone know where to insert the script?
Everything you need will be found in $radio_info. You can easily output this wherever you want:
echo htmlspecialchars($radio_info['now_playing']['artist'] . ' - ' . $radio_info['now_playing']['track']);
We download the stats in xml then process the stats daily buy importing to sql
// download icecast stats
$username = '';
$password = '';
$url = 'localhost:8000/stats';
$file = 'h:\\stream\\stats\\' . date('Ymd_His') . '.xml';
c:\\stream\\curl.exe --user $username:$password $url -o $file;
//Process to sql by looping through the stats folder and get filenames...
foreach(glob($path . "*.xml") as $filename) {
$content = file_get_contents($filename);
$filename = substr($filename, strlen($path), -4);
process($db, $filename, $content);
}
function process($db, $filename, $content) {
$xml = new SimpleXMLElement($content);
if(!preg_match('/^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})$/', $filename,
$date)) throw new Exception('Filename does not look like a date.');
foreach($xml->source as $source) {
$date_sql = $db->real_escape_string(sprintf("%04d-%02d-%02d %02d:%02d:%02d",
$date[1], $date[2], $date[3], $date[4], $date[5], $date[6]));
$stream_sql = $db->real_escape_string($source->genre);
$title_sql = $db->real_escape_string($source->title);
$listeners_sql = (int)$source->listeners;
$sql = "REPLACE INTO song_stats SET date_and_time = '$date_sql', stream =
'$stream_sql', title = '$title_sql', listeners = $listeners_sql";
$db->query($sql);
}
}
i have an array contain value like :
Check_Value ('level_codes', '1000', '1001','(1000,1002,1004)', 'DO ', '1')==1
i want to get values and store into an other parametrs like :
$codes = level_codes;
$first_value = 1000;
$second_value = 1001;
$list_values = (1000,1002,1004);
$action = DO;
$timer = 1;
$case = ==;
$status = 1;
is there any one please help me to do this work...!
Use the list() function: http://php.net/list
If the order is always the same, the following will reassign the values to the variables you want:
$parts = preg_split("/'?\(|\)'?/", $original_values);
$sub1 = explode(',', $parts[1]);
$codes = trim(str_replace("'", '', $sub1[0])) ;
$first_value = trim(str_replace("'", '', $sub1[1]));
$second_value = trim(str_replace("'", '', $sub1[2]));
$list_values = "($parts[2])";
$sub2 = explode(',', $parts[3]);
$action = trim(str_replace("'", '', $sub2[1]));
$timer = trim(str_replace("'", '', $sub2[2]));
preg_match('/(\D+)(\d+)/', $parts[4], $matches);
$case = $matches[1];
$status = $matches[2];
There might be a more elegant way to do it, but the original value is a string, rather than an array.
If the array is string, i think you should look into preg_match