Commit inicial con archivos existentes
This commit is contained in:
73
telegram/converters/HtmlToTelegramHtmlConverter.php
Executable file
73
telegram/converters/HtmlToTelegramHtmlConverter.php
Executable file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
class HtmlToTelegramHtmlConverter
|
||||
{
|
||||
/**
|
||||
* Convierte un fragmento de HTML a un formato compatible con el parse_mode=HTML de Telegram.
|
||||
*
|
||||
* @param string $html El HTML de entrada.
|
||||
* @return string El HTML adaptado para Telegram.
|
||||
*/
|
||||
public function convert($html)
|
||||
{
|
||||
if (empty(trim($html))) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 1. Decodificar entidades HTML como a espacios reales
|
||||
$html = html_entity_decode($html, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
|
||||
// 2. Reemplazar párrafos por saltos de línea para mantener la separación.
|
||||
// Quita la etiqueta de apertura y reemplaza la de cierre con un salto de línea.
|
||||
$html = str_ireplace('<p>', '', $html);
|
||||
$html = str_ireplace('</p>', "\n", $html);
|
||||
|
||||
// 3. Asegurar que <br> también se convierta en un salto de línea.
|
||||
$html = preg_replace('/<br\s*\/?>/i', "\n", $html);
|
||||
|
||||
// 4. Eliminar etiquetas no soportadas por Telegram, pero manteniendo las básicas y <img>.
|
||||
// Telegram soporta: <b>, <strong>, <i>, <em>, <u>, <ins>, <s>, <strike>, <del>, <a>, <code>, <pre>
|
||||
// Mantenemos <img> para que nuestro parser interno (TelegramSender) lo procese.
|
||||
$allowed_tags = '<b><strong><i><em><u><ins><s><strike><del><a><code><pre><img>';
|
||||
$html = strip_tags($html, $allowed_tags);
|
||||
|
||||
// 5. Limpiar múltiples saltos de línea consecutivos, pero NO los espacios en blanco.
|
||||
// Esto previene más de dos saltos de línea seguidos, que Telegram ignora.
|
||||
$html = trim($html);
|
||||
$html = preg_replace('/[\r\n]{3,}/', "\n\n", $html);
|
||||
|
||||
// 6. Corregir etiquetas que pudieran quedar abiertas (buena práctica).
|
||||
$html = $this->fixUnmatchedTags($html);
|
||||
|
||||
error_log("HTML convertido para Telegram: " . $html);
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Corrige etiquetas HTML no cerradas o mal formadas en el mensaje.
|
||||
* @param string $html El HTML a corregir.
|
||||
* @return string El HTML con las etiquetas corregidas.
|
||||
*/
|
||||
private function fixUnmatchedTags($html) {
|
||||
$tags = ['b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del', 'code', 'pre'];
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
// Contar etiquetas de apertura y cierre
|
||||
$openCount = substr_count(strtolower($html), "<$tag>");
|
||||
$closeCount = substr_count(strtolower($html), "</$tag>");
|
||||
|
||||
// Si hay más etiquetas de cierre que de apertura, eliminar las sobrantes
|
||||
if ($closeCount > $openCount) {
|
||||
$diff = $closeCount - $openCount;
|
||||
$html = preg_replace("#</{$tag}>#i", '', $html, $diff);
|
||||
}
|
||||
// Si hay más etiquetas de apertura que de cierre, agregar las que faltan al final
|
||||
elseif ($openCount > $closeCount) {
|
||||
$diff = $openCount - $closeCount;
|
||||
$html .= str_repeat("</{$tag}>", $diff);
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user