Reputation: 3523
aIs there a function in php (like Wordpress's the_content()) that can limit a text string without breaking the html tags that may be within it?
//example:
echo substr ('<p>this <a>is</a> a test</p>', 0, 10);
//output: "<p>this <a"
//desired output: "<p>this </p>"
Upvotes: 3
Views: 423
Reputation: 4210
No, there's not. But you could dig into Wordpress's source and see what the_content()
does to achieve that. I haven't done so myself. Preserving HTML tags in the shorter version will be a tricky block of code.
What do you need this for? Are you just trying to obtain a preview of a longer HTML blob? In the past I've done this by using a combination of strip_tags()
and substr()
, and wrapping the stripped section of content in a new <p>
tag.
Upvotes: 2