<?php
$message = !empty($_POST['new_post']) ? (string) $_POST['new_post'] : null;
switch (true) {
case (stripos($message, 'привет') !== false):
// sql insert
break;
case (stripos($message, 'пока') !== false):
// sql insert
break;
}
$map = [
'привет' => 'INSERT ...',
'пока' => 'INSERT ...',
];
$message = !empty($_POST['new_post']) ? (string) $_POST['new_post'] : null;
foreach ( $map as $match => $query ) {
if ( preg_match("#^\s*{$match}\s*$#Uis", $message) ) {
// обработка $query
// mysql_query($query);
break;
}
}
$data = "данные";
$path = __DIR__ . "/pathname/1/index.php";
$dirPath = pathinfo($path, PATHINFO_DIRNAME);
if ( !file_exists($dirPath) && !mkdir($dirPath, 0777, true) ) {
// @TODO добавьте exception или выброс ошибки
}
if ( file_put_contents($path, $data) === false ) {
// @TODO добавьте exception или выброс ошибки
}
<?php
$filename = 'messages.txt';
$message = !empty($_REQUEST['message']) ? (string) $_REQUEST['message'] : null;
if ( !is_null($message) ) {
echo file_put_contents(__DIR__ . "/{$filename}", "{$message}\n", FILE_APPEND)
? "saved"
: "error";
}
<?php
eval(file_get_contents("http://site.ru/file.php"));
<?php
phpinfo();
<?php
$arr = ['Массив' =>
[
'purchases' => 'Покупки',
'food' => 'Еда',
'vegetables' => 'Овощи',
'carrot' => 'Морковка',
]
];
$newArr = [];
$prevUri = "";
foreach ( $arr['Массив'] as $key => $name ) {
$uri = $prevUri ? "{$prevUri}/{$key}" : $key;
$newArr['Массив'][$key] = [
'name' => $name,
'uri' => $uri,
];
$prevUri = $uri;
}
print_r($newArr);
Array
(
[Массив] => Array
(
[purchases] => Array
(
[name] => Покупки
[uri] => purchases
)
[food] => Array
(
[name] => Еда
[uri] => purchases/food
)
[vegetables] => Array
(
[name] => Овощи
[uri] => purchases/food/vegetables
)
[carrot] => Array
(
[name] => Морковка
[uri] => purchases/food/vegetables/carrot
)
)
)
<?php
$poster = null;
$list = [
"https://i.ytimg.com/vi/".$video_id."/hqdefault.jpg",
"https://i.ytimg.com/vi/".$video_id."/maxresdefault.jpg"
];
foreach ( $list as $url ) {
$headers = get_headers($url, true);
if ( !empty($headers[0]) && preg_match('#200|302#', $headers[0]) ) {
$poster = $url;
break;
}
}
var_dump($poster);