This commit is contained in:
Roman Pyrih
2025-10-01 14:24:32 +02:00
parent a63e42a362
commit 0aa6f4c87c
3 changed files with 60 additions and 5 deletions

View File

@@ -257,3 +257,58 @@ function get_full_phone() {
wp_send_json_success(['html' => $html]);
}
if (!function_exists('split_text_into_spans')) {
function split_text_into_spans($content, $wordClass = 'word', $charClass = 'char') {
if (empty($content)) return '';
$doc = new DOMDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$doc->loadHTML('<?xml encoding="utf-8" ?><div id="wrapper">'.$content.'</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$textNodes = $xpath->query("//div[@id='wrapper']//text()[not(ancestor::script or ancestor::style or ancestor::svg or ancestor::code or ancestor::pre or ancestor::iframe)]");
foreach ($textNodes as $node) {
$text = $node->nodeValue;
if (!preg_match('/\S/u', $text)) continue;
$fragment = $doc->createDocumentFragment();
$words = preg_split('/(\s+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($words as $word) {
if (trim($word) === '') {
$fragment->appendChild($doc->createTextNode($word));
continue;
}
$wordSpan = $doc->createElement('span');
$wordSpan->setAttribute('class', $wordClass);
$chars = preg_split('//u', $word, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $ch) {
$span = $doc->createElement('span');
$span->setAttribute('class', $charClass);
$span->appendChild($doc->createTextNode($ch));
$wordSpan->appendChild($span);
}
$fragment->appendChild($wordSpan);
}
$node->parentNode->replaceChild($fragment, $node);
}
$wrapper = $doc->getElementById('wrapper');
$html = '';
foreach ($wrapper->childNodes as $child) {
$html .= $doc->saveHTML($child);
}
return $html;
}
}

View File

@@ -22,7 +22,7 @@
<?php echo $box1['title']; ?>
</div>
<div class="col col-2">
<p><?php echo $box1['text']; ?></p>
<p class="animate-text"><?php echo split_text_into_spans($box1['text']); ?></p>
</div>
</div>
</div>
@@ -77,9 +77,9 @@
</div>
</div>
<div class="row row-2">
<div class="box-info">
<div class="box-info animate-text">
<h3><?php echo $box3['info_title']; ?></h3>
<?php echo $box3['info_text']; ?>
<?php echo split_text_into_spans($box3['info_text']); ?>
</div>
</div>
</div>

View File

@@ -141,8 +141,8 @@
<div class="container">
<div class="row">
<div class="col col-1" box-title="<?php echo $box5['mini_title']; ?>">
<div class="box-text">
<?php echo $box5['title']; ?>
<div class="box-text animate-text">
<?php echo split_text_into_spans($box5['title']); ?>
</div>
<ul>
<a class="btn_4" href="<?php echo esc_url($box5['btn']['url']); ?>" target="<?php echo esc_attr($box5['btn']['target'] ?: '_self'); ?>">