Page 1 of 1

Text Manipulation

Posted: Wed Dec 25, 2002 9:58 pm
by Jim
I'm pullling headlines from a database, and I'd like to display the first 16 or so characters of the headline followed by three periods (...).

Any idea how to do that?

Thanks!

Posted: Thu Dec 26, 2002 12:11 am
by evilcoder
OK, first off:

Start by making a conection to your database. I assume you know how that is done.

Now for the actual code.

Start by making a table. I have made a very simple one here.

Code: Select all

<table width="100%">
Then, before the <tr> you want the headlines to repeat in put this code

Code: Select all

<?php
$headline_txt_limit = "16"; //Number of characters before ...
$headline_db_query = mysql_query("SELECT headline_field FROM db_table");
while ($headline_array = mysql_fetch_array($headline_db_query)){
$headline = $headline_array["headline_field"];
$headline_o = $headline;
$headline = substr($headline,0,$headline_txt_limit);
if ($headline_o <> $headline) 
{
$headline.= "...";
}
?>
Now for the HTML of the Table Row:

Code: Select all

&lt;tr&gt;
    &lt;td&gt; &lt;?=$headline ?&gt; &lt;/td&gt;
  &lt;/tr&gt;
Once thats done end the open while statement with:

Code: Select all

<?php
}
?>
Now finish your table

Code: Select all

&lt;/table&gt;
&lt;/html&gt;
And thats it!

Posted: Thu Dec 26, 2002 6:05 am
by zacky

Code: Select all

<?
$string = "This is a example script... =)";
$formated = substr_replace($string, '...', 16);
echo $formated;
?>
this will output: This is a exampl...

:)