I'm having problems with php serialization, when ever ' " are included i get an extra character space. Ex if it's 6 characters i get 7
$episodes_count = sizeof($episode_title);
$episodes = array();
$content = '';
for ($i = 0; $i <= $episodes_count; $i++) {
$title = htmlspecialchars($episode_title[$i], ENT_QUOTES);
$airdate = $episodes_airdates[$i];
$season = $episodes_seasons[$i];
$number = $episodes_numbers[$i];
$plot = $episodes_plot[$i];
// check if empty
if (!empty($title) && !empty($number ) && !empty($plot )) {
$episodes[] = array(
'title' => $title,
'airdate' => $airdate,
'season' => $season,
'number' => $number,
'plot' => $plot,
);
}
}
// Serialized Episodes in case they exist, if not, remove the goal post
if ( sizeof($episodes) > 0 ) {
$content = str_replace("'", '%',serialize($episodes));
}
update_post_meta($post_id, 'episodes', $content);
}
You pass some data that looks serialized to wordpress to update_post_meta - wordpress can have a problem with that and the data will get broken while stored and retrieved.
To prevent that, you can prefix the string so that it does not look serialized any longer or you even encode the whole string, e.g. with base64_encode. That will prevent wordpress and other components to modify the values due to encoding issues.
Related
I'm using a Sendinblue SMTP into my PHP project and I want to send transactionals emails to a dynamic list of emails, the problem is that I have a syntax error when I'm using a variable instead a string. For example, this code works great:
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo(
array(
'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''
)
)->
setFrom('sender#sender.com', 'Test')->
setReplyTo('sender#sender.com', 'Test')->
setSubject('Example')->
setText('Test')->
setHtml($htmlContent);
$res = $mailin->send();
print_r($res);
But if I use a variable instead the Strings in "addTo Array" it shows syntax error, for example:
$customers = '';
foreach ($clientes as $customer) {
for ($i=1; $i < 41; $i++) {
if ($customer['email'.$i] != "" or $customer['email'.$i] != NULL) {
$customers .= "'".$customer['email'.$i]. "' => '', " ; //for each customer's email add the email in " 'email#email.com' => '', " format
}
}
}
$customers = substr($customers, 0, -2); //removes last space and comma of the String
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo(
array(
$customers
)
)->
setFrom('sender#sender.com', 'Test')->
setReplyTo('sender#sender.com', 'Test')->
setSubject('Example')->
setText('Test')->
setHtml($htmlContent);
$res = $mailin->send();
print_r($res);
If I use the Print_r($customers) function it shows the exact string that I used in my first example, even if I use the code:
$text = "'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''";
if ($customers == $text) {
print_r("Yes");
}else{
print_r("No");
}
The result is "Yes", but when I use the variable in
addTo(
array(
$customers
)
)->
shows an error, but if I use the string directly the email is sent
addTo(
array(
'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''
)
)->
And I don't know why it shows error if the $customers variable has the string that is needed.
Do you know how to use the variable with the emails that I need to send?
You don't build an array by concatenating strings with => in them. To create an element in an associative array, just assign to that array index.
$customers = [];
foreach ($customers as $customer) {
for ($i = 1; $i < 41; $i++) {
if (!empty($customer["email" . $i])) {
$customers[$customer["email" . $i]] = "";
}
}
}
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo($customers)->
...
Also, see Why non-equality check of one variable against many values always returns true? for why you should have used && rather than || when you were skipping empty emails (which I've simplified by using !empty()).
I use CodeIgniter, and when an insert_batch does not fully work (number of items inserted different from the number of items given), I have to do the inserts again, using insert ignore to maximize the number that goes through the process without having errors for existing ones.
When I use this method, the kind of data I'm inserting does not need strict compliance between the number of items given, and the number put in the database. Maximize is the way.
What would be the correct way of a) using insert_batch as much as possible b) when it fails, using a workaround, while minimizing the number of unnecessary requests?
Thanks
The Correct way of inserting data using insert_batch is :
CI_Controller :
public function add_monthly_record()
{
$date = $this->input->post('date');
$due_date = $this->input->post('due_date');
$billing_date = $this->input->post('billing_date');
$total_area = $this->input->post('total_area');
$comp_id = $this->input->post('comp_id');
$unit_id = $this->input->post('unit_id');
$percent = $this->input->post('percent');
$unit_consumed = $this->input->post('unit_consumed');
$per_unit = $this->input->post('per_unit');
$actual_amount = $this->input->post('actual_amount');
$subsidies_from_itb = $this->input->post('subsidies_from_itb');
$subsidies = $this->input->post('subsidies');
$data = array();
foreach ($unit_id as $id => $name) {
$data[] = array(
'date' => $date,
'comp_id' => $comp_id,
'due_date' => $due_date,
'billing_date' => $billing_date,
'total_area' => $total_area,
'unit_id' => $unit_id[$id],
'percent' =>$percent[$id],
'unit_consumed' => $unit_consumed[$id],
'per_unit' => $per_unit[$id],
'actual_amount' => $actual_amount[$id],
'subsidies_from_itb' => $subsidies_from_itb[$id],
'subsidies' => $subsidies[$id],
);
};
$result = $this->Companies_records->add_monthly_record($data);
//return from model
$total_affected_rows = $result[1];
$first_insert_id = $result[0];
//using last id
if ($total_affected_rows) {
$count = $total_affected_rows - 1;
for ($x = 0; $x <= $count; $x++) {
$id = $first_insert_id + $x;
$invoice = 'EBR' . date('m') . '/' . date('y') . '/' . str_pad($id, 6, '0', STR_PAD_LEFT);
$field = array(
'invoice_no' => $invoice,
);
$this->Companies_records->add_monthly_record_update($field,$id);
}
}
echo json_encode($result);
}
CI_Model :
public function add_monthly_record($data)
{
$this->db->insert_batch('monthly_record', $data);
$first_insert_id = $this->db->insert_id();
$total_affected_rows = $this->db->affected_rows();
return [$first_insert_id, $total_affected_rows];
}
AS #q81 mentioned, you would split the batches (as you see fit or depending on system resources) like this:
$insert_batch = array();
$maximum_items = 100;
$i = 1;
while ($condition == true) {
// code to add data into $insert_batch
// ...
// insert the batch every n items
if ($i == $maximum_items) {
$this->db->insert_batch('table', $insert_batch); // insert the batch
$insert_batch = array(); // empty batch array
$i = 0;
}
$i++;
}
// the last $insert_batch
if ($insert_batch) {
$this->db->insert_batch('table', $insert_batch);
}
Edit:
while insert batch already splits the batches, the reason why you have "number of items inserted different from the number of items given" might be because the allowed memory size is reached. this happened to me too many times.
I have this xml file named flight-itinerary.xml. A scaled-down version is shown below.
<itin line="1" dep="LOS" arr="ABV">
<flt>
<fltav>
<cb>1</cb>
<id>C</id>
<av>10</av>
<cur>NGN</cur>
<CurInf>2,0.01,0.01</CurInf>
<pri>15000.00</pri>
<tax>30800.00</tax>
<fav>1</fav>
<miles></miles>
<fid>11</fid>
<finf>0,0,1</finf>
<cb>2</cb>
<id>J</id>
<av>10</av>
<cur>NGN</cur>
<CurInf>2,0.01,0.01</CurInf>
<pri>13000.00</pri>
<tax>26110.00</tax>
<fav>1</fav>
<miles></miles>
<fid>12</fid>
<finf>0,0,0</finf>
</fltav>
</flt>
</itin>
The complete file contains 8 itinerary <itin> elements. The <fltav> element of each of the <itin> elements contains 11 of the <cb>1</cb> to <finf>0,0,1</finf> groups.
And below is the code I am using to process the file:
<?php
function processFlightsData()
{
$data = array();
$dom= new DOMDocument();
$dom->load('flight-itinerary.xml');
$classbands = $dom->getElementsByTagName('classbands')->item(0);
$bands = $classbands->getElementsByTagName('band');
$itineraries = $dom->getElementsByTagName('itin');
$counter = 0;
foreach($itineraries AS $itinerary)
{
$flt = $itinerary->getElementsByTagName('flt')->item(0);
$dep = $flt->getElementsByTagName('dep')->item(0)->nodeValue;
$arr = $flt->getElementsByTagName('arr')->item(0)->nodeValue;
$time_data = $flt->getElementsByTagName('time')->item(0);
$departure_day = $time_data->getElementsByTagName('ddaylcl')->item(0)->nodeValue;
$departure_time = $time_data->getElementsByTagName('dtimlcl')->item(0)->nodeValue;
$departure_date = $departure_day. ' '. $departure_time;
$arrival_day = $time_data->getElementsByTagName('adaylcl')->item(0)->nodeValue;
$arrival_time = $time_data->getElementsByTagName('atimlcl')->item(0)->nodeValue;
$arrival_date = $arrival_day. ' '. $arrival_time;
$flight_duration = $time_data->getElementsByTagName('duration')->item(0)->nodeValue;
$flt_det = $flt->getElementsByTagName('fltdet')->item(0);
$airline_id = $flt_det->getElementsByTagName('airid')->item(0)->nodeValue;
$flt_no = $flt_det->getElementsByTagName('fltno')->item(0)->nodeValue;
$flight_number = $airline_id. $flt_no;
$airline_type = $flt_det->getElementsByTagName('eqp')->item(0)->nodeValue;
$stops = $flt_det->getElementsByTagName('stp')->item(0)->nodeValue;
$av_data = $flt->getElementsByTagName('fltav')->item(0);
$cbs = iterator_to_array($av_data->getElementsByTagName('cb')); //11 entries
$ids = iterator_to_array($av_data->getElementsByTagName('id')); //ditto
$seats = iterator_to_array($av_data->getElementsByTagName('av')); //ditto
$curr = iterator_to_array($av_data->getElementsByTagName('cur')); //ditto
$price = iterator_to_array($av_data->getElementsByTagName('pri')); //ditto
$tax = iterator_to_array($av_data->getElementsByTagName('tax')); //ditto
$miles = iterator_to_array($av_data->getElementsByTagName('miles')); //ditto
$fid = iterator_to_array($av_data->getElementsByTagName('fid')); //ditto
$inner_counter = 0;
for($i = 0; $i < count($ids); $i++)
{
$data[$counter][$inner_counter] = array
(
'flight_number' => $flight_number,
'flight_duration' => $flight_duration,
'departure_date' => $departure_date,
'departure_time' => substr($departure_time, 0, 5),
'arrival_date' => $arrival_date,
'arrival_time' => substr($arrival_time, 0, 5),
'departure_airport_code' => $dep,
'departure_airport_location_name' => get_airport_data($dep, $data_key='location'),
'arrival_airport_code' => $arr,
'arrival_airport_location_name' => get_airport_data($arr, $data_key='location'),
'stops' => $stops,
'cabin_class' => $ids[$i]->nodeValue,
'ticket_class' => $ids[$i]->nodeValue,
'ticket_class_nicename' => formate_ticket_class_name($ids[$i]->nodeValue),
'available_seats' => $seats[$i]->nodeValue,
'currency' => $curr[$i]->nodeValue,
'price' => $price[$i]->nodeValue,
'tax' => $tax[$i]->nodeValue,
'miles' => $miles[$i]->nodeValue,
);
++$inner_counter;
}
return $data;
}
?>
Now, the outer loop iterates 8 times for each <itin> element, and during each iteration of the outer loop, the inner loop iterates 11 times, resulting in a total of 88 iterations per pass and causing serious performance issues. What I am looking for is a faster method of processing the file. Any helps will be greatly appreciated.
I don't think the loop is the bottle-neck. You should check your operations that are called within the loop, get_airport_data and formate_ticket_class_name.
Trying your code (without the auxiliary operations) on a number of itin elements takes less than a second, check this fiddle: http://phpfiddle.org/main/code/7fpi-b3ka (Note that the XML might not be similar to yours, I've guessed a lot of elements that were missing).
If there are operations that are called which increases the processing time substantially, try to call the operation with bulk data or cache the responses.
I'm trying to filter an array (derived from a json object), so as to return the array key based on the value. I'm not sure if array search $key = array_search($value, $array); is the best way to do this (I can't make it work), and I think there must be a better way.
So far I've got this, but it isn't working. Grateful for any help!
public function getBedroomData(array $data,$num_beds = null,$type) {
$data = (array) $data;
if($num_beds > 0) {
$searchstring = "avg_".$num_beds."bed_property_".$type."_monthly";
} else {
$searchstring = "avg_property_".$type."_monthly";
}
$avg_string = array_search($data, $searchstring);
return $avg_string;
}
The array consists of average property prices taken from the nestoria api as follows:
http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json
This returns a long json object. My problem is that the data isn't consistent - and I'm looking for the quickest (run time) way to do the following:
$data['response']['metadata']['0'] //= data to return, [0] unknown
$data['response']['metadata']['0']['metadata_name'] = "avg_1bed_property_rent_monthly" //= string I know!
$data['response']['metadata']['1'] //= data to return, [1] unknown
$data['response']['metadata']['1']['metadata_name'] = "avg_1bed_property_buy_monthly" //= string I know!
$data['response']['metadata']['2'] = //= data to return, [2] unknown
$data['response']['metadata']['2']['metadata_name'] = "avg_2bed_property_buy_monthly" //= string I know!
.....
.....
.....
$data['response']['metadata']['10'] = avg_property_rent_monthly
$data['response']['metadata']['11'] = avg_property_buy_monthly
$data['response']['metadata'][most_recent_month] = the month reference for getting the data from each metadata list..
It isn't possible to filter the initial search query by number of bedrooms as far as I can work out. So, I've just been array slicing the output to get the information I've needed if bedrooms are selected, but as the data isn't consistent this often fails.
To search inside that particular json response from nestoria, a simple foreach loop can be used. First off, of course call the json data that you need. Then, extract the whole data, the the next step if pretty straightforward. Consider this example:
$url = 'http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json';
$contents = file_get_contents($url);
$data = json_decode($contents, true);
$metadata = $data['response']['metadata'];
// dummy values
$num_beds = 1; // null or 0 or greater than 0
$type = 'buy'; // buy or rent
function getBedroomData($metadata, $num_beds = null, $type) {
$data = array();
$searchstring = (!$num_beds) ? "avg_property_".$type."_monthly" : "avg_".$num_beds."bed_property_".$type."_monthly";
$data['metadata_name'] = $searchstring;
$data['data'] = null;
foreach($metadata as $key => $value) {
if($value['metadata_name'] == $searchstring) {
$raw_data = $value['data']; // main data
// average price and data points
$avg_price = 0;
$data_points = 0;
foreach($raw_data as $index => $element) {
$avg_price += $element['avg_price'];
$data_points += $element['datapoints'];
}
$data_count = count($raw_data);
$price_average = $avg_price / $data_count;
$data_points_average = $data_points / $data_count;
$data['data'][] = array(
'average_price' => $price_average,
'average_datapoints' => $data_points_average,
'data' => $raw_data,
);
}
}
return $data;
}
$final = getBedroomData($metadata, $num_beds, $type);
print_r($final);
id like to break a css file to php array.
i have this code:
function parseCss($css_str) {
$css = array();
// TODO include external css
$css_str = preg_replace('/(#import\s+["\'].+["\'];)/', "", $css_str);
// Strip all line endings and both single and multiline comments
$css_str = preg_replace('/\s*(?!<\")\/\*+[^\*]+\*+\/(?!\")\s*/', "", $css_str);
$css_class = explode("}", $css_str);
while(list($key, $value) = each($css_class)){
$aCSSObj = explode("{", $value);
$cssSelector = strtolower(trim($aCSSObj[0]));
if($cssSelector){
// regular class not in media query
$cssprops[] = $cssSelector;
$a = explode(";", $aCSSObj[1]);
while(list($key, $val0) = each($a)){
if(trim($val0)){
$aCSSSub = explode(":", $val0);
$cAtt = strtolower(trim($aCSSSub[0]));
if(isset($aCSSSub[1])){
$aCSSItem[$cAtt] = trim($aCSSSub[1]);
}
}
}
if((isset($css[$cssSelector])) && ($css[$cssSelector])){
$aCSSItem = array_merge($css[$cssSelector], $aCSSItem);
}
$css[$cssSelector] = (isset($aCSSItem)) ? $aCSSItem : null ;
unset($aCSSItem);
}
if(strstr($cssSelector, ",") && !strstr($cssSelector, "#media")){
$aTags = explode(",", $cssSelector);
print_r($aTags);
foreach($aTags as $key0 => $value0){
$css[$value0] = $css[$cssSelector];
}
unset($css[$cssSelector]);
}
}
unset($css_str, $css_class, $aCSSSub, $aCSSItem, $aCSSObj);
return $css;
}
but that doesn't return me a css file with media queries like it supposed to be.
and if there is a ":" sign in the css value -for example an IE expression like that:
top:expression((-20+(document.documentElement.clientHeight ?document.documentElement.clientHeight/2:document.body.clientHeight/2)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body.scrollTop))+'px')
its giving back cutted code.
so the result should look like that:
Array
(
[selector] => array (
[prop] => value
[prop2] => value
)
[#media handheld,only screen and (max-width:320px)] => array(
[selector] => array(
[prop] => value
[prop2] => value
)
)
)
how do i make it work?
Solved that!
eventually I've used CSSTidy to parse the css, and it does the job beautifully!
http://csstidy.sourceforge.net/index.php