Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting chars for a content

Tags:

php

I have content, in which I'm trying to limit using this query:

if(strlen($news_content < 35)){
         $news_content = substr($news_content, 0, 30) . "";
    }

But for some reason, it isn't limiting the chars.. Or It's my coding(which I'm sure is why it's not working) that's off a bit.

here's the full code:

<?php

    $amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); 
    $comments = mysql_num_rows($amount_get);            

    $grab = mysql_query("SELECT * FROM articles WHERE id='" . mysql_real_escape_string($_GET['id']) . "' LIMIT 1");    
    $grab = mysql_query("SELECT id, news_title, news_content, news_author, news_day, news_month, news_year, news_date FROM articles ORDER BY id DESC limit 3"); 
    $news_day =$row['news_day'];
    $news_month =$row['news_month'];
    $news_year =$row['news_year'];
    $news_content =$row['news_content'];

    if(strlen($news_content < 35)){
                        $news_content = substr($news_content, 0, 30) . "";
                    } 

    elseif (mysql_num_rows($grab)==0) {
    echo "<div class='alert alert-note-x'>Sorry, it looks like their are no articles posted!</div>";
}
    while($row = mysql_fetch_array($grab)){

?>

<div class="post-container">
    <div class="post">
    <div class="left">
    <div class="date"><span class="day"><?php echo $row['news_day'] ?></span> <span class="month"><?php echo $row['news_month'] ?></span> <span class="year"><?php echo $row['news_year'] ?></span></div></div>
    <div class="postcontent"><h5 class="posttitle"><a href="#/media/<?php echo $row['id'] ?>.<?php echo stripslashes(str_replace(" ", "-", $row['news_title'])); ?>"><?php echo stripslashes($row['news_title']); ?></a></h5>                       

        <?php echo $row['news_content'] ?>
        <p> (READ MORE)</p>
        <p>Comments (<?php echo $comments ?>)</p>
        </div>
</div>​
</div>  

I'm simply trying to limit the "news_content", but I don't know where it is that I'm messing up at. Thanks!

like image 995
TrippedStackers Avatar asked Jun 10 '26 04:06

TrippedStackers


1 Answers

Try changing the first line from:

if(strlen($news_content < 35)) {...}

To:

if(strlen($news_content) > 35) {

That includes switching the < to > , as presumably you want to truncate if it's longer than 35 characters rather than less ?

I think generally there are better ways to truncate though. Your approach could just cut text off in the middle of a word abruptly. Have a look at http://www.the-art-of-web.com/php/truncate/#.UNEbnmdaerI or http://www.ambrosite.com/blog/truncate-long-titles-to-the-nearest-whole-word-using-php-strrpos for other ideas.

like image 120
Michael Low Avatar answered Jun 15 '26 23:06

Michael Low



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!