Salar
Salar

Reputation: 5509

detect not closed html tags in a text, and fix it

I have an html text called news which is generated by ckeditor, so it may contain html tags like bold, italic, paragraph and....
I want to choose the first 100 characters of it and show them to the users of my app. but choosing the first 100 characters of news may cause to choose an html text which has unclosed tags(the closing tags of my html text may be after the character number 100).
is there any PHP or js function to parse a text and fix the unclosed html tags or at least remove unclosed html tags?

Upvotes: 1

Views: 411

Answers (3)

Mayurkumar Vandra
Mayurkumar Vandra

Reputation: 21

You could remove the HTML Tags by using the PHP function strip_tags().

<?php
$text = '<h1>Test Header. <b>On Example</b>';
echo strip_tags($text);

// Output: Test Header. On Example

?>

Please check the reference on the function at https://secure.php.net/strip_tags

Side Note:

I have a function to chopContent that you could use, to get the first 100 characters:

public function chopContent($content, $content_length = 100)

{

    if (strlen($content) > $content_length) {
        $offset = ($content_length - 13) - strlen($content);
        $content = mb_substr($content, 0, strrpos($content, ' ', $offset)) . '...';
    }

    return $content;
}

Upvotes: 0

Roshan Padole
Roshan Padole

Reputation: 408

Tidy is a binding for the Tidy HTML clean and repair utility which allows you to not only clean and otherwise manipulate HTML documents, but also traverse the document tree.

Try this

Upvotes: 0

Joe Majewski
Joe Majewski

Reputation: 1641

As far as removing tags altogether, the strip_tags function in PHP should do the trick.

strip_tags($input, '<br><br/>');

The second argument is the allowed tags (the ones that don't get stripped).

Upvotes: 2

Related Questions