Created
June 15, 2025 20:30
-
-
Save fabiomb/d89726dd05f5114e17c78b992c111def to your computer and use it in GitHub Desktop.
ProcessText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function processText($text) { | |
| // Arrays para almacenar elementos encontrados | |
| $mentions = []; | |
| $hashtags = []; | |
| $urls = []; | |
| $youtubeVideos = []; | |
| // Patrones regex | |
| $mentionPattern = '/@(\w+)/'; | |
| $hashtagPattern = '/#(\w+)/'; | |
| $urlPattern = '/\b(?:https?:\/\/)?(?:www\.)?([a-z0-9-]+(?:\.[a-z0-9-]+)*(?:\.[a-z]{2,})(?:\/[^\s]*)?)/i'; | |
| $youtubePattern = '/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})/i'; | |
| // Almacenar los iframes de YouTube y sus tokens | |
| $youtubeReplacements = []; | |
| // Paso 1: Procesar URLs de YouTube y reemplazarlas con tokens únicos | |
| $processedText = preg_replace_callback($youtubePattern, function($match) use (&$youtubeReplacements, &$urls, &$youtubeVideos) { | |
| $fullUrl = $match[0]; | |
| $videoId = $match[1]; | |
| // Generar un token único | |
| $token = '<<YOUTUBE_' . uniqid() . '>>'; | |
| // Crear el iframe | |
| $iframe = sprintf( | |
| '<div class="video-container"><iframe width="560" height="315" src="https://www.youtube.com/embed/%s" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>', | |
| htmlspecialchars($videoId) | |
| ); | |
| // Guardar el reemplazo para más tarde | |
| $youtubeReplacements[$token] = $iframe; | |
| // Guardar URL e ID | |
| $urls[] = $fullUrl; | |
| $youtubeVideos[] = $videoId; | |
| return $token; | |
| }, $text); | |
| // Paso 2: Procesar URLs normales y protegerlas con tokens | |
| $urlReplacements = []; | |
| $processedText = preg_replace_callback($urlPattern, function($match) use (&$urls, $youtubePattern, &$urlReplacements) { | |
| $url = $match[0]; | |
| // Ignorar si es un token de YouTube | |
| if (strpos($url, '<<YOUTUBE_') === 0) { | |
| return $url; | |
| } | |
| // Ignorar si es una URL de YouTube (aunque esto no debería ocurrir ya) | |
| if (preg_match($youtubePattern, $url)) { | |
| return $url; | |
| } | |
| $urls[] = $url; | |
| // Asegurar que la URL tenga el protocolo | |
| $fullUrl = (strpos($url, 'http') === 0) ? $url : 'https://' . $url; | |
| // Generar token único para esta URL | |
| $token = '<<URL_' . uniqid() . '>>'; | |
| $linkHtml = sprintf( | |
| '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', | |
| htmlspecialchars($fullUrl), | |
| htmlspecialchars($url) | |
| ); | |
| // Guardar el reemplazo | |
| $urlReplacements[$token] = $linkHtml; | |
| return $token; | |
| }, $processedText); | |
| // Paso 3: Procesar menciones (las URLs están protegidas con tokens) | |
| preg_match_all($mentionPattern, $processedText, $mentionMatches); | |
| $mentions = $mentionMatches[1]; | |
| $processedText = preg_replace_callback($mentionPattern, function($match) { | |
| $username = $match[1]; | |
| return sprintf( | |
| '<a href="../usuario/com-%s" class="mention">@%s</a>', | |
| htmlspecialchars($username), | |
| htmlspecialchars($username) | |
| ); | |
| }, $processedText); | |
| // Paso 4: Restaurar URLs procesadas | |
| foreach ($urlReplacements as $token => $linkHtml) { | |
| $processedText = str_replace($token, $linkHtml, $processedText); | |
| } | |
| // Paso 5: Procesar hashtags | |
| preg_match_all($hashtagPattern, $processedText, $hashtagMatches); | |
| $hashtags = $hashtagMatches[1]; | |
| $processedText = preg_replace_callback($hashtagPattern, function($match) { | |
| $hashtag = $match[1]; | |
| return sprintf( | |
| '<a href="hashtag/%s" class="hashtag">#%s</a>', | |
| htmlspecialchars($hashtag), | |
| htmlspecialchars($hashtag) | |
| ); | |
| }, $processedText); | |
| // Paso 6: Restaurar los iframes de YouTube | |
| foreach ($youtubeReplacements as $token => $iframe) { | |
| $processedText = str_replace($token, $iframe, $processedText); | |
| } | |
| return [ | |
| 'processedText' => $processedText, | |
| 'mentions' => array_unique($mentions), | |
| 'hashtags' => array_unique($hashtags), | |
| 'urls' => array_unique($urls), | |
| 'youtubeVideos' => array_unique($youtubeVideos) | |
| ]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment