Jump to content


Photo

character limiting with PHP


  • Please log in to reply
7 replies to this topic

#1 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 October 2006 - 11:09 AM

Hi

I've been on a steep learning curve with PHP and My SQL - and I think this is a PHP question.

I am pulling news from the database to a frame, where I want to show the date, headline, subhead and an amount of the story that will fit in the frame - probably limited to around 200 charcters or so... the main text area it is pullin from though vcan have up to 2000 chracters in it. How do I limit the output?

I'm at this pont so far which works apart from limiting the text...

<? $query  = "SELECT * FROM News LIMIT 0,1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo"<h6>{$row['Date']}&nbsp;&nbsp;&nbsp;";echo "<br /><A Href='{$row['Newsid']}'>{$row['Heading']}</A>";echo "<br>{$row['Subhead']}";echo "<br />{$row['Main']}</h6>";}?>

I dare say I'll need help with the output file from the links as well - but thats for another post ;)

#2 vbnullchar

vbnullchar
  • Members
  • PipPipPip
  • Advanced Member
  • 428 posts
  • LocationPasig City, Philippines

Posted 10 October 2006 - 11:13 AM

try this

function trunc($details,$max) {
if(strlen($details)>$max) {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details."...";
    }
    return $details;
}
Registered Linux User #399942
Ubuntu User #14134
--
my blog

#3 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 October 2006 - 11:42 AM

I'm sure what you put is fab - but I am a true noob! How would I integrate this with the code I already employ as stated above?

I've tried placing this (and changing the $details to $Main - and $max to $200 which suit the table I am working from) and putting it where the last ;echo call is - with no joy - and then just running your script without any of mine - but that also doesn't work.

Probably silly questions I know - but any advice?


try this

function trunc($details,$max) {
if(strlen($details)>$max) {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details."...";
    }
    return $details;
}



#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 11:56 AM

You need to call the trunc() function with some values.  Using the code that both of you posted, I'd try something like this...

<?php

$query  = "SELECT * FROM News LIMIT 0, 1";
$result = mysql_query($query);

function trunc($details, $max){
   if (strlen($details) > $max){
      $details = substr($details, 0, $max);
      $i = strrpos($details," ");
      $details = substr($details, 0, $i);
      $details = $details."...";
   }
   return $details;
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   $main = trunc($row['Main'], 200); // You see the call here to the above function
   echo <<<HTML
   <h6>{$row['Date']}<br><a href="{$row['Newsid']}">{$row['Heading']}</a><br>{$row['Subhead']}<br>{$main}</h6>
HTML;
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 October 2006 - 12:44 PM

Thanks to you both - with the application example/advice it makes a lot more sense now - cheers Huggie!

Some of us need our hands held through this horrible learning curve :)

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 12:46 PM

No problem, that's what we're here for!  :D

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 stuart.cole

stuart.cole
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 12 October 2006 - 04:19 PM

On the same note ...

What if I need to limit the character count more than once?

If so, what is the best way to do it? I've tried what seems logical and have got stuck with the below which (obviously to those who understand it) doesn't work.

Thoughts?

<? $query  = "SELECT * FROM News ORDER BY Time DESC LIMIT 0, 1";

$result = mysql_query($query);

function trunc($details, $max){
   if (strlen($details) > $max){
      $details = substr($details, 0, $max);
      $i = strrpos($details," ");
      $details = substr($details, 0, $i);
      $details = $details."...";
   }
   return $details;
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   $main = trunc($row['Main'], 200);{
   $heading = trunc($row['Heading'], 75);{
   $subhead = trunc($row['Subhead'], 100); // You see the call here to the above function
   echo <<<HTML
   {$row['Date']}<br><a href="newsitem.php?id=
{$row['Newsid']}">{$heading}</a><br>{$subhead}<br>{$main}
HTML;
}
?></div>


#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 October 2006 - 09:51 AM

You have a couple of rogue characters there.  Remove the '{' from the end of the following lines...

$main = trunc($row['Main'], 200);{
$heading = trunc($row['Heading'], 75);{

Let me know if you still have issues after this :)

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users