xitas
xitas

Reputation: 1164

Get limited word of a string

So I am getting a sting from database which contains my html tags. Now I don't want the whole string I just wanted some part of it so I used substr() to short my string now it is showing my string with html tags. I don't want to show my html tags.

This is how I am doing:

<?php 
    $content = $news['content']; 
    echo substr($content, 0, 100)."...";
?>

Result:

<p class="boldp">  Overall Employer in World and No.1 in Pharmaceutical i...  

Desire Result:

 Overall Employer in World and No.1 in Pharmaceutical i...     

How can I remove these html tags from my string.

I don't know if anyone have asked this question or not. I tried finding solution to this but I didn't find anything.

Upvotes: 1

Views: 673

Answers (2)

tzafar
tzafar

Reputation: 664

//Function Limiting words.....

function limit_words($string, $word_limit)
{
    $words = explode(' ', $string);
    if (count($words) > $word_limit):
        return implode(' ', array_slice($words, 0, $word_limit)) . "...";
    else:
        return implode(' ', array_slice($words, 0, $word_limit));
    endif;
}

//Function Limiting characters.....

function limit_characters($string, $length = 100, $append = "&hellip;")
{
    $string = trim($string);
    if (strlen($string) > $length) {
        $string = wordwrap($string, $length);
        $string = explode("\n", $string);
        $string = array_shift($string) . $append;
    }
    return $string;
}

Function to clean text content and do the following

/*
* Strip HTML Tags
* Clean up things like &amp;
* Strip out any url-encoded stuff
* Replace non-AlphaNumeric characters with space
* Replace Multiple spaces with single space
* Trim the string of leading/trailing space
*/

function clean_text($text)
{
    return trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($text))))));
}

Upvotes: 2

baao
baao

Reputation: 73221

I think you are looking for strip_tags:

echo strip_tags($news['content']);

Upvotes: 2

Related Questions