php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of php.php

  1. <?php
  2.  
  3. $message = $data['message'];
  4.             $text = $message['text'];
  5.  
  6.  
  7.  
  8.  
  9.             if (isset($message['entities'])) {
  10.                 $offset = 0;
  11.  
  12.                 foreach ($message['entities'] as $entity) {
  13.                     $type = $entity['type'];
  14.                     $length = $entity['length'];
  15.                     $start = $entity['offset'];
  16.  
  17.                     // Извлекаем текст, соответствующий entity
  18.                     $entity_text = substr($text, $start + $offset, $length);
  19.  
  20.                     // Заменяем entity на HTML-разметку в зависимости от типа
  21.                     switch ($type) {
  22.                         case 'text_link':
  23.                             $url = $entity['url'];
  24.                             // Заменяем ссылку на HTML-разметку с текстом анкора
  25.                             $link = "<a href='$url'>$entity_text</a>";
  26.                             $text = substr_replace($text, $link, $start + $offset, $length);
  27.                             // Учитываем смещение после замены
  28.                             $offset += strlen($link) - $length;
  29.                             break;
  30.                         case 'bold':
  31.                             // Заменяем жирный текст на HTML-разметку
  32.                             $bold_text = "<b>$entity_text</b>";
  33.                             $text = substr_replace($text, $bold_text, $start + $offset, $length);
  34.                             // Учитываем смещение после замены
  35.                             $offset += strlen($bold_text) - $length;
  36.                             break;
  37.                         case 'italic':
  38.                             // Заменяем курсивный текст на HTML-разметку
  39.                             $italic_text = "<i>$entity_text</i>";
  40.                             $text = substr_replace($text, $italic_text, $start + $offset, $length);
  41.                             // Учитываем смещение после замены
  42.                             $offset += strlen($italic_text) - $length;
  43.                             break;
  44.                         case 'strikethrough':
  45.                             // Заменяем текст с зачеркиванием на HTML-разметку
  46.                             $strikethrough_text = "<s>$entity_text</s>";
  47.                             $text = substr_replace($text, $strikethrough_text, $start + $offset, $length);
  48.                             // Учитываем смещение после замены
  49.                             $offset += strlen($strikethrough_text) - $length;
  50.                             break;
  51.                         case 'code':
  52.                             // Заменяем код на HTML-разметку
  53.                             $code_text = "<code>$entity_text</code>";
  54.                             $text = substr_replace($text, $code_text, $start + $offset, $length);
  55.                             // Учитываем смещение после замены
  56.                             $offset += strlen($code_text) - $length;
  57.                             break;
  58.                         case 'underline':
  59.                             // Заменяем подчеркнутый текст на HTML-разметку
  60.                             $underline_text = "<u>$entity_text</u>";
  61.                             $text = substr_replace($text, $underline_text, $start + $offset, $length);
  62.                             // Учитываем смещение после замены
  63.                             $offset += strlen($underline_text) - $length;
  64.                             break;
  65.                         case 'spoiler':
  66.                             // Заменяем спойлер на HTML-разметку
  67.                             $spoiler_text = "<spoiler>$entity_text</spoiler>";
  68.                             $text = substr_replace($text, $spoiler_text, $start + $offset, $length);
  69.                             // Учитываем смещение после замены
  70.                             $offset += strlen($spoiler_text) - $length;
  71.                             break;
  72.                         // Добавьте обработку других типов разметки по аналогии
  73.                     }
  74.                 }
  75.             }
  76.  
File Description
  • php
  • PHP Code
  • 09 Sep-2023
  • 4.38 Kb
You can Share it: