Hallo,

How to trim content? I would like to show only the first 30 letters of this content.

This is what I have so far:

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

while ($data = mysql_fetch_array($result)){

echo '<div id="text">'.$data['content'].'</div>'.'<br>';

}

Thanks before.

This is the function that I use, but I typically truncate a string by the word.

function stringTrim($string, $length){
    $string = (strlen($string) > $lenth) ? substr($string,0,$length) . '...' : $string;
    return $string;
}

CSS Tricks has a couple of example of functions that do that http://css-tricks.com/snippets/php/truncate-string-by-words/

Hello,

I try this:

    include('include/con_database.php');

    $result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

    while ($data = mysql_fetch_array($result)){

        echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
    }

I do not see the content appears.

I tested your above code in my local pc. It works fine.Make sure that "$data['content']" having contents or check your database table structure.

This works fine:

echo '<div id="text">'.$data['content'].'</div>'.'<br>';

It just does not cut the text into the first 30 letters. but this:

echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';

does not works. I wonder why?

You'll probably need to elaborate some more on "does not work", does it give an error back, or just the full string?

That function I posted before was something I wrote quick because I couldn't find the other function that I had wrote for trimming on character or word.

Here is the full function that will trim a string on character or word.

function stringTrim($string, $length, $split = 'word', $append = ' ...'){

    if($split == 'word'){

        $string_array = explode(' ', $string);

        if(count($string_array) > $length && $length > 0){
          $string = implode(' ', array_slice($string_array, 0, $length)) . $append;
          return $string;
        }
    }

    elseif($split == 'char'){
        $string = (strlen($string) > $length) ? substr($string,0,$length) . $append : $string;
        return $string;
    }

    else{
        return $string;
    }

}

Then you would just call it like stringTrim($string, 30, 'char') for trimming the string by the character. If you leave out the 'char' in the function, it will default to trimming by the word.

This is what I have now:

index.php

<div id="news">LATEST NEWS</div>

<?php

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE id='124'");

while ($data = mysql_fetch_array($result)){

echo '<div id="banner">';
echo '<div id="title">';
echo '<h1>'.'<a href="http://localhost/IndonusaCMS/events.php">'.$data['title'].'</a>'.'</h1>';
echo '</div>';

echo '<div id="desc">';
echo '<td>'.'<div id="images">'.'<img src="images/events/thumb/' . $data['images'] . '">'.'</div>'.'</td>'.'<br>';

echo '<div id="text">'.stringTrim($data['content'], 30, 'char').'</div>'.'<br>';

/*
echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
*/
echo '</div>';
echo '</div>';

}

?>

<?php 



    function stringTrim($string, $length, $split = 'word', $append = ' ...'){
    if($split == 'word'){
    $string_array = explode(' ', $string);
    if(count($string_array) > $length && $length > 0){
    $string = implode(' ', array_slice($string_array, 0, $length)) . $append;
    return $string;
    }
    }
    elseif($split == 'char'){
    $string = (strlen($string) > $length) ? substr($string,0,$length) . $append : $string;
    return $string;
    }
    else{
    return $string;
    }
    }

?>

Now, nothing appears on the front page ( I mean the content does not appears on the front page ).

note: I copy and paste the function exactly as it is and use it directly.

This is also does not work, I wonder why?

index.php

<?php

include('include/con_database.php');

$result = mysql_query("SELECT * FROM dynamic_content WHERE CatID='4'");

while ($data = mysql_fetch_array($result)){

$string = $data['content'];

echo '<div id="banner">';
echo '<div id="title">';
echo '<h1>'.'<a href="http://localhost/IndonusaCMS/events.php?id='.$data['ID'].'">'.$data['title'].'</a>'.'</h1>';
echo '</div>';

echo '<div id="images">'.'<img src="images/events/thumb/' . $data['images'] . '">'.'</div>'.'<br>';

echo '<div id="desc">';
/* echo '<div id="text">'.stringTrim($data['content'], 30, 'char').'</div>'.'<br>'; */
echo substr($string, 0, 30); 

/*
echo '<div id="text">'.substr($data['content'], 0, 30).'</div>'.'<br>';
*/
echo '</div>';
echo '</div>';

}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.