Text Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Text Manipulation

Post 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!
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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!
zacky
Forum Newbie
Posts: 19
Joined: Fri Nov 29, 2002 6:08 am

Post 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...

:)
Post Reply