Last active
January 8, 2021 10:17
-
-
Save Alex-Space/f37a498bb6e808da30fa to your computer and use it in GitHub Desktop.
doc PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://getjump.github.io/ru-php-the-right-way/ - Правильный путь | |
http://www.php.su/ - русский портал по изучению php | |
интерполяция - процесс преобразования имен переменных в их значения ("$var текст") | |
# Выводить textarea, в которой пробелы заменяются br | |
nl2br() | |
# Отсортировать один массив по значениям другого (меняет сам массив $menus) | |
usort( $menus, function($val1, $val2) use ($option) { | |
return (array_search($val1->term_id, $option) > array_search($val2->term_id, $option)); | |
}); | |
# Подключение к базе данных | |
try { | |
$pdo = new PDO('mysql:host=host-name-here; dbname=data-base-name-here', 'user-name-here', 'password-here'); | |
} catch (PDOException $e) { | |
$output = "Невозможно подколючиться к серверу баз данных."; | |
include 'output.html.php'; # Подключаем шаблон, в котором вывод ошибок | |
exit(); | |
} | |
# Регулярка для получения айдишника ютуба | |
$url = "http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=relate"; | |
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars ); | |
echo $my_array_of_vars['v']; | |
// Output: C4kxS1ksqtw | |
# Добавить 3 точки в конец текста | |
function add3dots( $string, $repl, $limit ) { | |
if(strlen($string) > $limit) { | |
return substr($string, 0, $limit) . $repl; | |
} | |
else { | |
return $string; | |
} | |
} | |
# Получить русские называния месяцев с датой | |
function rudate( $date ) { | |
$trans = array( | |
"January" => "Январь", | |
"February" => "Февраль", | |
"March" => "Март", | |
"April" => "Апрель", | |
"May" => "Май", | |
"June" => "Июнь", | |
"July" => "Июль", | |
"August" => "Август", | |
"September" => "Сентябрь", | |
"October" => "Октябрь", | |
"November" => "Ноябрь", | |
"December" => "Декабрь", | |
); | |
$result = strtr( $date, $trans); | |
return $result; | |
} | |
# Удалить куку со всего сайта (вставлять можно в wp-settings.php) | |
function wpm_cut_path( $path ) { | |
$pattern = '/\/[^\/]+\/?$/'; | |
preg_match( $pattern, $path, $matches ); | |
if ( isset( $matches ) && ! empty( $matches[0] ) ) { | |
$new_path = str_replace( $matches[0], '', $path ); | |
return $new_path; | |
} else { | |
return false; | |
} | |
} | |
function wpm_recursive_remove_cookie( $path ) { | |
if ( $path ) { | |
setcookie('gravity_shop_mode', '', -1, $path ); | |
setcookie('gravity_shop_mode', '', time()-1000, $path ); | |
$path = wpm_cut_path( $path ); | |
return wpm_recursive_remove_cookie( $path ); | |
} else { | |
return false; | |
} | |
} | |
$path = $_SERVER['REQUEST_URI']; | |
wpm_recursive_remove_cookie( $path); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment