Получение данных с помощью функций preg_match()
и preg_match_all()
.
Извлечение содержимого из круглых, квадратных и фигурных скобок:
$text = '
Телеобъектив: диафрагма [ƒ/2.8]
Широкоугольный объектив: (диафрагма ƒ/1.8)
По беспроводной сети: {до 13 часов}
Поддержка диапазона: <Dolby Vision и HDR10>
';
/* [...] */
preg_match_all("/\[(.+?)\]/", $text, $matches);
print_r($matches[1]);
/* (...) */
preg_match_all("/\((.+?)\)/", $text, $matches);
print_r($matches[1]);
/* {...} */
preg_match_all("/\{(.+?)\}/", $text, $matches);
print_r($matches[1]);
/* <...> */
preg_match_all("/\<(.+?)\>/", $text, $matches);
print_r($matches[1]);
Результат:
Array
(
[0] => ƒ/2.8
)
Array
(
[0] => диафрагма ƒ/1.8
)
Array
(
[0] => до 13 часов
)
Array
(
[0] => Dolby Vision и HDR10
)
$text = '
<title>Тег TITLE</title>
<h1>Тег H1</h1>
<p>Текст 1</p>
<p>Текст 2</p>
';
/* <title> */
preg_match('/<title[^>]*?>(.*?)<\/title>/si', $text, $matches);
echo $matches[1];
/* <h1> */
preg_match('/<h1[^>]*?>(.*?)<\/h1>/si', $text, $matches);
echo $matches[1];
/* Извлекает текст из всех <p> */
preg_match_all('/<p[^>]*?>(.*?)<\/p>/si', $text, $matches);
print_r($matches[1]);
Результат:
Тег TITLE
Тег H1
Array
(
[0] => Текст 1
[1] => Текст 2
)
$text = 'Text http://ya.ru text http://google.ru text.';
preg_match_all('/(http:\/\/|https:\/\/)?(www)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-\?\%\&]*)*\/?/i', $text, $matches);
print_r($matches[0]);
Результат:
Array
(
[0] => http://ya.ru
[1] => http://google.ru
)
$text = '
<a href="http://ya.ru">Яндекс</a>
<a href="http://google.ru">Google</a>
<a href="http://mail.ru">Mail.ru</a>
';
preg_match_all('/<a.*?href=["\'](.*?)["\'].*?>/i', $text, $matches);
print_r($matches[1]);
Результат:
Array
(
[0] => http://ya.ru
[1] => http://google.ru
[2] => http://mail.ru
)
$text = '
<a href="http://ya.ru">Яндекс</a>
<a href="http://google.ru">Google</a>
<a href="http://mail.ru">Mail.ru</a>
';
preg_match_all('/<a.*?>(.*?)<\/a>/i', $text, $matches);
print_r($matches[1]);
Результат:
Array
(
[0] => Яндекс
[1] => Google
[2] => Mail.ru
)
$text = 'text <img alt="" src="/logo.png"> text';
preg_match_all('/<img.*src="(.*)".*>/is', $text, $matches);
print_r($matches[1]);
Результат:
Array
(
[0] => /logo.png
)
$text = 'text admin@mail.ru text text text admin@ya.ru';
preg_match_all('/([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,6}/i', $text, $matches);
print_r($matches[0]);
Результат:
Array
(
[0] => admin@mail.ru
[1] => admin@ya.ru
)
HEX/HEXA
$css = '
body {
color: #000;
background: #4545;
}
header {
color: #111111;
background: #00000080;
}
';
preg_match_all('/#(?:[0-9a-f]{3,8})/i', $css, $matches);
print_r($matches[0]);
Результат:
Array
(
[0] => #000
[1] => #4545
[2] => #111111
[3] => #00000080
)
RGB/RGBA
$css = '
body {
color: rgb(0,0,0);
background: rgba(17,85,68,0.33);
}
header {
color: rgb(17,17,17);
background: rgba(0,0,0,0.5);
}
';
preg_match_all('/((rgba)\((\d{1,3},\s?){3}(1|0?\.?\d+)\)|(rgb)\(\d{1,3}(,\s?\d{1,3}){2}\))/i', $css, $matches);
print_r($matches[0]);
Array
(
[0] => rgb(0,0,0)
[1] => rgba(17,85,68,0.33)
[2] => rgb(17,17,17)
[3] => rgba(0,0,0,0.5)
)