class Config {
private static $configData;
public static function get($parameter = null, $default = null)
{
if (is_null(self::$configData)) {
self::$configData = include '../config.php';
}
if ($parameter) {
return self::$configData[$parameter] ?? $default;
}
return self::$configData;
}
}
// в клиентском методе
public function someMethod() {
$config = Config::get();
}
<?php
// в config.php
return [
'param' => 'value',
];
?>
$str = '<div class="stove-preview__main-img" style="background-image: url(\'/upload/resize_cache/iblock/114/245_600_2.jpg\')>';
preg_match('/background-image:\s*url\([\'"]?(?<image_url>[^\'")]+)[\'"]?\s*\)/', $str, $matches);
$imageUrl = $matches['image_url'] ?? null;
// /upload/resize_cache/iblock/114/245_600_2.jpg
$array = [
['value' => 'photo-wallpapers', 'label' => 'Фотообои'],
['value' => 'fresco', 'label' => 'Фреска'],
['value' => 'panno', 'label' => 'Панно'],
['value' => 'fresco', 'label' => 'Test'],
['value' => 'ttt', 'label' => 'WoW'],
];
function sortArrayByFresco(array &$array) {
usort($array, function ($a, $b) {
$isFrescoA = $a['value'] === 'fresco';
$isFrescoB = $b['value'] === 'fresco';
if ($isFrescoA && $isFrescoB) {
return 0;
}
return $isFrescoA ? -1 : $isFrescoB;
});
}
// Test
sortArrayByFresco($array);
print_r($array);
/*
Array
(
[0] => Array
(
[value] => fresco
[label] => Фреска
)
[1] => Array
(
[value] => fresco
[label] => Test
)
[2] => Array
(
[value] => photo-wallpapers
[label] => Фотообои
)
[3] => Array
(
[value] => panno
[label] => Панно
)
[4] => Array
(
[value] => ttt
[label] => WoW
)
)
*/
function extractPathElements(string $path): array
{
$explodedPath = explode('/', $path);
$pathTree = [];
foreach ($explodedPath as $key => $value) {
if (!$value) {
continue;
}
$pathTree[] = [
'file_name' => $value,
'parent_file' => $explodedPath[$key-1] ?? '',
];
}
return $pathTree;
}
// client code
var_dump(extractPathElements('/dir1/dir2/file.txt'));
/*
array(3) {
[0]=>
array(2) {
["file_name"]=>
string(4) "dir1"
["parent_file"]=>
string(0) ""
}
[1]=>
array(2) {
["file_name"]=>
string(4) "dir2"
["parent_file"]=>
string(4) "dir1"
}
[2]=>
array(2) {
["file_name"]=>
string(8) "file.txt"
["parent_file"]=>
string(4) "dir2"
}
}
*/
$user = User::where('login', $postData['login'])->first(); // На примере ORM Eloquent, тут ищется и возвращается экземпляр класса-модели пользователя с этим логином.
if (!$user || !password_verify($postData['password'], $user->password)) {
throw new UserLoginException('Неверный логин или пароль!');
} else {
$_SESSION['user_id'] = $user->id;
Http::redirect('/profile');
}
$var = '<div class="news-box2">
<img class="news-img" src="' . $new->img . '" alt="">
<div class="news-title-g">' . StringHelper::truncate($new->title,25,'...') . '</div>
</div>';
$var = <<<EOL
<div class="news-box2">
<img class="news-img" src="{$new->img}" alt="">
<div class="news-title-g">{StringHelper::truncate($new->title,25,'...')}</div>
</div>
EOL;
ob_start(); ?>
<div class="news-box2">
<img class="news-img" src="<?= $new->img ?>" alt="">
<div class="news-title-g"><?= StringHelper::truncate($new->title,25,'...') ?></div>
</div>
<?php
$var = ob_get_contents();
ob_end_clean();
header('Content-type: text/html; charset=utf-8');
function print_arr($arr){
echo '<pre>' . print_r($arr, true) . '</pre>';
}
function get_content($url, bool $isPost = false, $data = []){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($isPost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookie.txt');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36');
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
$url_auth = 'https://www.facebook.com/login';
$url = 'https://www.facebook.com/groups/websarafan.ru';
$auth_data = [
'email' => '',
'pass' => '',
];
$data = get_content($url_auth, true, $auth_data);
$data = get_content($url, false);
var_dump($data);