Глава 16: черновик

This commit is contained in:
Igor Simdyanov
2022-05-07 17:26:05 +03:00
parent 4c91cf9b42
commit db3a62ff0c
19 changed files with 120 additions and 0 deletions

4
strings/explode.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$str = 'Имя, Фамилия, e-mail';
echo '<pre>';
print_r(explode(', ', $str)); // ['Имя', 'Фамилия', 'e-mail']

View File

@ -0,0 +1,8 @@
<?php
$text = <<<html
<script language="JavaScript">
alert("Приветик!");
</script>
html;
echo htmlspecialchars($text);

3
strings/implode.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$arr = ['Сидоров', 'Иван', 'Петрович'];
echo implode(', ', $arr); // Сидоров, Иван, Петрович

8
strings/javascript.php Normal file
View File

@ -0,0 +1,8 @@
<?php
$text = <<<html
<script language="JavaScript">
alert("Приветик!");
</script>
html;
echo $text;

6
strings/json_decode.php Normal file
View File

@ -0,0 +1,6 @@
<?php
$json = '{"employee":"Иван Иванов","phones":["916 153 2854","916 643 8420"]}';
$arr = json_decode($json, true);
echo '<pre>';
print_r($arr);
echo '</pre>';

9
strings/json_encode.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$arr = [
'employee' => 'Иван Иванов',
'phones' => [
'916 153 2854',
'916 643 8420'
]
];
echo json_encode($arr);

View File

@ -0,0 +1,9 @@
<?php
$arr = [
'employee' => 'Иван Иванов',
'phones' => [
'916 153 2854',
'916 643 8420'
]
];
echo json_encode($arr, JSON_UNESCAPED_UNICODE);

3
strings/locale_try.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$loc = setlocale(LC_ALL, 'ru', 'ru_RU', 'rus', 'russian');
echo "На этой системе русская локаль имеет имя '$loc'";

6
strings/localeconv.php Normal file
View File

@ -0,0 +1,6 @@
<?php
setlocale(LC_ALL, 'ru_RU.UTF-8');
echo '<pre>';
print_r(localeconv());
echo '</pre>';

9
strings/nl2br.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$str = <<<text
hello
php
text;
echo $str;
echo '<br /><br />';
echo nl2br($str);

2
strings/printf.php Normal file
View File

@ -0,0 +1,2 @@
<?php
printf('Первое число - %d', 26); // Первое число - 26

5
strings/printf_color.php Normal file
View File

@ -0,0 +1,5 @@
<?php
$red = 255;
$green = 255;
$blue = 100;
printf('#%X%X%X', $red, $green, $blue); // #FFFF64

4
strings/printf_float.php Normal file
View File

@ -0,0 +1,4 @@
<?php
echo '<pre>';
printf('%8.2f\n', 1000.45684); // 1000.46
printf('%.2f\n', 12.92869); // 12.93

5
strings/printf_int.php Normal file
View File

@ -0,0 +1,5 @@
<?php
echo '<pre>';
printf('% 4d\n', 45); // ' 45'
printf('%04d\n', 45); // '00045'
echo '</pre>';

View File

@ -0,0 +1,9 @@
<?php
$number = 5867;
printf('Двоичное число: %b<br />', $number);
printf('Десятичное число: %d<br />', $number);
printf('Число с плавающей точкой: %f<br />', $number);
printf('Восьмеричное число: %o<br />', $number);
printf('Строковое представление: %s<br />', $number);
printf('Шестнадцатеричное число (нижний регистр): %x<br />', $number);
printf('Шестнадцатеричное число (верхний регистр): %X<br />', $number);

12
strings/serialize.php Normal file
View File

@ -0,0 +1,12 @@
<?php
$numbers = [23, 45, 34, 2, 12];
// Упаковываем массив в строку
$str = serialize($numbers);
echo "$str<br />";
// Извлекаем массив из строки
$arr = unserialize($str);
echo '<pre>';
print_r($arr); // [23, 45, 34, 2, 12]
echo '</pre>';

10
strings/strip_tags.php Normal file
View File

@ -0,0 +1,10 @@
<?php
$str = <<<html
<p>Параграф.</p>
<!-- Comment -->
Еще немного текста
html;
echo htmlspecialchars(strip_tags($str));
echo '<br />';
echo htmlspecialchars(strip_tags($str, '<p>'));

3
strings/wordwrap.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$str = 'Здесь может быть любой текст';
echo wordwrap($str, 10, '<br />');

View File

@ -0,0 +1,5 @@
<?php
$str = "Некорректный для UTF-8 символ \x80";
echo 'ENT_IGNORE: ' . htmlspecialchars($str, ENT_IGNORE) . '<br />';
echo 'ENT_SUBSTITUTE: ' . htmlspecialchars($str, ENT_SUBSTITUTE) . '<br />';
echo 'ENT_DISALLOWED: ' . htmlspecialchars($str, ENT_DISALLOWED) . '<br />';