Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateTableIndex(&$html)
- {
- /*
- Автоматически сформировать “Указатель таблиц”. Работает как оглавление, но
- ссылки делаются на таблицы в документе. Текст ссылки такой:
- Таблица <номер> “содержимое первой ячейки из таблицы”
- */
- // Находим все таблицы в HTML
- preg_match_all('/<(table\b[^>]*)>/sui', $html, $tables);
- $indexList = '<ul>';
- foreach ($tables[1] as $index => $tableHead) {
- $tableId = 'table_' . ($index + 1);
- $pattern = '/<' . preg_quote($tableHead, '/') . '>(.*?)<\/?table.*>/s';
- preg_match($pattern, $html, $matches);
- $table_content = $matches[0]; // извлечение найденного содержимого таблицы
- // Находим содержимое первой ячейки таблицы
- preg_match('/<t[drh][^>]*>(.*?)<\/t[drh]>/is', $table_content, $firstCell);
- $firstCellContent = trim(strip_tags($firstCell[1])); // Получаем текст из ячейки без HTML-тегов
- // print_r($firstCellContent);
- // Добавляем ссылку на таблицу в индекс
- $indexList .= '<li><a href="#' . $tableId . '">Таблица ' . ($index + 1) . ' "' . $firstCellContent . '"</a></li>';
- $rep_smpl = '/<' . preg_quote($tableHead, '/') . '>/';
- $rep_on = '<' . $tableHead . ' id="' . $tableId . '">';
- // Добавляем идентификатор таблицы для якоря
- $new_table_content = preg_replace($rep_smpl, $rep_on, $table_content, 1);
- $html = str_replace($table_content, $new_table_content, $html);
- }
- $indexList .= '</ul>';
- return $indexList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement