loadHTML('' . $html, LIBXML_NONET)) return ''; $body = $dom->getElementsByTagName('body')->item(0); if (!$body) return ''; $parts = []; $walk = static function (DOMNode $node) use (&$walk, &$parts): void { if ($node instanceof DOMText) { $parts[] = preg_replace('/\s+/u', ' ', $node->nodeValue ?? ''); return; } if (!($node instanceof DOMElement)) return; $tag = strtolower($node->tagName); if (in_array($tag, ['head', 'script', 'style', 'noscript', 'svg', 'form', 'template'], true)) return; $block = in_array($tag, ['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'tr', 'td', 'th', 'section', 'article', 'blockquote'], true); if ($block || $tag === 'br') $parts[] = "\n"; foreach ($node->childNodes as $child) $walk($child); if ($block) $parts[] = "\n"; }; $walk($body); return implode('', $parts); } finally { libxml_clear_errors(); libxml_use_internal_errors($previous); } } function PAReviewCapitalizationFindings(string $text, string $location = 'Message'): array { $findings = []; $seen = []; foreach (preg_split('/\R/u', $text) ?: [] as $line) { $line = trim(preg_replace('/[\t ]+/u', ' ', $line)); if ($line === '' || preg_match('/^(?:https?:\/\/|www\.|\*\|)/i', $line)) continue; preg_match_all('/(?:\A|[.!?]\s+)([\p{Ll}][\pL\pM\'-]*)/u', $line, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[1] as [$word, $offset]) { if (substr($line, $offset + strlen($word), 1) === '@') continue; $before = substr($line, 0, $offset); if (preg_match('/\b(?:e\.g|i\.e|etc|vs|mr|mrs|ms|dr)\.\s*$/i', $before)) continue; $suggested = mb_strtoupper(mb_substr($word, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($word, 1, null, 'UTF-8'); $key = $location . "\0" . $line . "\0" . $offset; if (isset($seen[$key])) continue; $seen[$key] = true; $context = mb_strlen($line, 'UTF-8') > 150 ? mb_substr($line, 0, 147, 'UTF-8') . '...' : $line; $findings[] = ['current' => $word, 'suggested' => $suggested, 'context' => $context, 'location' => $location]; } } return $findings; } function PAReviewEmailCapitalizationFindings(array $template): array { $subject = trim((string)($template['publish_subject'] ?? '')); if ($subject === '') $subject = (string)($template['subject'] ?? ''); $findings = PAReviewCapitalizationFindings($subject, 'Subject'); $html = trim((string)($template['publish_code'] ?? '')); if ($html === '') $html = (string)($template['code'] ?? ''); $body = PAReviewVisibleEmailText($html); if ($body === '') { $body = trim((string)($template['publish_text'] ?? '')); if ($body === '') $body = (string)($template['text'] ?? ''); } return array_merge($findings, PAReviewCapitalizationFindings($body, 'Email body')); }