×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
title
element from $xml
function value_in($element_name, $xml, $content_only = true) {
if ($xml == false) {
return false;
}
$found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.
'</'.$element_name.'>#s', $xml, $matches);
if ($found != false) {
if ($content_only) {
return $matches[1]; //ignore the enclosing tags
} else {
return $matches[0]; //return the full pattern match
}
}
// No match found: return false.
return false;
}
$title = value_in('title', $xml);
price
value$inventory = array(
array("type"=>"fruit", "price"=>3.50),
array("type"=>"milk", "price"=>2.90),
array("type"=>"pork", "price"=>5.43),
);
The solution is to use array_multisort
function$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
In PHP 5.5 or later array_column
function can be used$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
thousands_separator
is ,
which is 4th parameter.
Without passing something else for the thousands_separator
parameter, the latency
was equal to 1,511.68525
The following PHP code caused problem in MySQL
$this->latency = number_format(microtime(1)-$_SESSION['startTime'], 5);
MySQL error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'latency' at row 1
Solution to the problem is to specify the 4th parameter as an empty string ""
to get latency
equal to 1511.68525
$this->latency = number_format(microtime(1)-$_SESSION['startTime'], 5, '.', '');
Authorization type: Basic Auth
Username: usr
Password: pwd
PHP cURL requestcurl_setopt($ch, CURLOPT_USERPWD, "usr:pwd");
time limit
for file_get_contents
is 60 seconds which we can increase using timeout
property of http
object $opts = [
"http" => [
"method" => "GET",
"timeout"=> 1200,
"header" =>
"Channel-Name: ".CHANNEL."\r\n" .
"Authorization: ".AUTHORIZATION."\r\n"
]
];
// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts); //, false, $context
$startTime = microtime(1);
$res = file_get_contents($url . $query, false, $context);