Я хочу усечь некоторый текст (загруженный из базы данных или текстового файла), но он содержит HTML, поэтому в результате теги включаются и меньше текста будет возвращено. Это может привести к тому, что теги не будут закрыты или частично закрыты (поэтому Tidy может работать неправильно, а контент еще меньше). Как я могу обрезать на основе текста (и, вероятно, останавливаться, когда вы попадаете в таблицу, поскольку это может вызвать более сложные проблемы).
substr("Hello, my <strong>name</strong> is <em>Sam</em>. I´m a web developer.",0,26)."..."
Результат:
Hello, my <strong>name</st...
Я хочу:
Hello, my <strong>name</strong> is <em>Sam</em>. I´m...
Как я могу это сделать?
В то время как мой вопрос заключается в том, как это сделать в PHP, было бы хорошо знать, как это сделать на С#... либо должно быть в порядке, поскольку я думаю, что смог бы переносить этот метод (если это не так) встроенный метод).
Также обратите внимание, что я включил объект HTML ´
- который должен рассматриваться как один символ (а не 7 символов, как в этом примере).
strip_tags
является резервным, но я бы потерял форматирование и ссылки, и у него все еще возникнет проблема с объектами HTML.