Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KikoLdasd's avatar

How to use v-html with substring

Hello.

How can I use v-html with substring function? I want to limit for example a description but if I use

v-html="text.substring(0,100)"

That substring will limit the html characters and the description will not appear. How can I use v-html and at the same time limit the text?

0 likes
4 replies
tykus's avatar

v-html expects a String; not an expression; besides, why would you expect the first 100 characters of an arbitrary HTML content to be valid HTML at all?

What problem are you actually trying to solve?

tykus's avatar

@KikoLdasd my point is (i) v-html takes a String and (ii) you cannot guarantee that the first 100 characters are valid HTML - if you only have text content then use v-text directive instead, and pass to it a computed property which gets the first 100 characters.

Niush's avatar

Like such simply extract just the text: https://stackoverflow.com/a/28900362/6453292

function extractContent(html) {
    return new DOMParser()
        .parseFromString(html, "text/html")
        .documentElement.textContent;
}

But, instead of using the extractContent function each time inside v-html. I would suggest to store summary of your html content in excerpt column in the database table, when creating or editing. Then, load the excerpt directly where needed.

Please or to participate in this conversation.