Home » Php » php – Removing markdown formatting for quick excerpt

php – Removing markdown formatting for quick excerpt

Posted by: admin July 12, 2020 Leave a comment


Anyone know of a script or already built function that will help me create a quick excerpt using already existing markdown formatted content?

I am looking at formatting results like StackOverflow where I have the title, and a short excerpt, for basically news view. I want to remove the markdown fully and just preserve the few words.

My concern is that I am going to use a codeigniter helper word_limiter()to limit the output. Which may create broken markdown:

**This is a sentence which has some _markdown_ that is [cutoff..

So I’m not sure how to go about it.

All my content is stored with markdown (not HTML formatting) otherwise I would do ‘striptags’ etc;

I’ve found something similar for Ruby on SO here: Truncate Markdown?, but I would love something for PHP.

I’m just not sure if I should just strip all *_[]()> characters basically? But if there is a URL (split in half), I’m worried it will look nasty.

Thoughts? I’ve looked around a bit, but at first glance haven’t found anything that strips markdown.

How to&Answers:

Consider marking-up the text, then running strip_tags.

— Update —

There are a couple of other options, if you do not want to mark-up the text just to strip tags, you can:

  1. Introduce a regex to strip out the markdown elements

  2. Store the content in the database with NO markup/markdown, just the text

  3. If you mark-up then strip_tags, you can store the results in memcache to avoid the extra overhead