Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converter.php
1<?php
2
3namespace Bitrix\Main\Mail;
4
6{
7 public static function htmlToText(string $body): string
8 {
9 $body = str_replace(array("\n","\r"), '', $body);
10 // get <body> inner html if exists
11 $innerBody = trim(preg_replace('/(.*?<body[^>]*>)(.*?)(<\/body>.*)/is', '$2', $body));
12 $body = $innerBody ?: $body;
13
14 // modify links to text version
15 $body = preg_replace_callback(
16 "%<a[^>]*?href=(['\"])(?<href>[^\1]*?)(?1)[^>]*?>(?<text>.*?)<\/a>%ims",
17 function ($matches)
18 {
19 $href = $matches['href'];
20 $text = trim($matches['text']);
21 if (!$href)
22 {
23 return $matches[0];
24 }
25 $text = strip_tags($text);
26 return ($text ? "$text:" : '') ."\n$href\n";
27 },
28 $body
29 );
30
31 // change <br> to new line
32 $body = preg_replace('/<br(\s*)?\/?>/i', "\n", $body);
33
34 $body = preg_replace('|(<style[^>]*>)(.*?)(<\/style>)|isU', '', $body);
35 $body = preg_replace('|(<script[^>]*>)(.*?)(<\/script>)|isU', '', $body);
36
37 // remove tags
38 $body = strip_tags($body);
39
40 // format text to the left side
41 $lines = [];
42 foreach (explode("\n", trim($body)) as $line)
43 {
44 $lines[] = trim($line);
45 }
46
47 // remove redundant new lines
48 $body = preg_replace("/[\\n]{2,}/", "\n\n", implode("\n", $lines));
49
50 // remove redundant spaces
51 $body = preg_replace("/[ \\t]{2,}/", " ", $body);
52
53 // decode html-entities
54 return html_entity_decode($body);
55 }
56}
static htmlToText(string $body)
Definition converter.php:7