Strip Markdown from Kirbytext

I’m pretty sure there’s a solve for this, but I haven’t been able to figure it out right now…

So I have a number of pages that are loading dynamic content (Kirbytext formatted), and then at times I want to display that content without any formatting (eg, inside a twitter description or in an XML page), just text only.

I can display the text as just plain text, except that will maintain the **bold** and [links](http://url) and ###Headlines.

How do I take a field and echo just plain text, without the markdown elements? I’ve tried a number of combinations from the cheatsheet, no luck yet.

For reference, here’s what I’m trying to do.
`

<?php echo xml($item->text()->kirbytext()) ?>

`
but that generates the html.

1 Like

For

look at kirbytext($text).

Try to delete it or change it to xml($text).

But I never have tested the change in this scenario.

Or use $field->escape()

If I use kirbytext() anywhere in the echo, it’ll return html. If I don’t use kirbytext(), it’ll return markdown elements I don’t want, like brackets and asterisks and whatnot.

Update: Tried adding ->escape(), which broke the xml doc.

How about using kirbytext() and afterwards strip the tags: http://php.net/manual/en/function.strip-tags.php

1 Like

You could also try using str::unhtml() from the toolkit.

str::unhtml($item->text()->kt());
1 Like

Hey guys.

Thanks a bunch for both suggestions. The strip_tags() method seems like the way to go! Here’s what I got…

<?php
  // other variables up here
  $desc = strip_tags($item->text()->kirbytext());
?>
<description><?php echo $desc; ?></description>

I also tried str::unhtml($item->text()->kt()); but that would either provide invalid XML or give a 500 error.

But anyway, big thanks to you both.

2 Likes