Limiting Strings in PHP

Miscellaneous Forums/General Discussion/Limiting Strings in PHP

I'm using PHP to build my website, and I'm having a little problem. I extract the information for my journal from a SQL database and translate it to "$row['content']," "$row['title']," and others, like date, etc. I'm trying to limit the content row to 200 characters. Does anyone know how to do this?

By limiting the content row to 200 chars, do you mean showing only the first 200 chars from a lot of text, or do you mean setting a maximum character limit the content field can store?
Do you use phpMyAdmin?

If you are getting the data from a web page, you can set the maxlength attribute of the input field.

you can also use php to chop the end off the string with the substr(<string>, <start>, <length>) command
something like:
$string = "my value"
$string = substr($string, 0,6)


Like this :
$content = substr( $row['content'],0,200);


You might want to also look at this site : http://uk2.php.net/substr

Thanks, indiepath!

OK, now I'm having another problem:
http://www.noenemies.com/journal.php

For some reason, it doesn't show any of the journal entries. It is supposed to show a list of journal entries (with the content limited to 200 characters), but it doesn't. And it's not supposed to show the comment box. Here's my PHP code:
<?php
  include_once('header.html');

  if (isset($_REQUEST['view'])) {
    $p_id = $_REQUEST['view'];
  } else {
    $p_id = '';
  }
?>

<div id="content">

<?php

if ($p_id = ''){
  
  $query = "SELECT id, title, content, DATE_FORMAT(date, '%M %d, %Y') AS date FROM journal ORDER BY id DESC";
  $result = mysql_query($query);
  $first = TRUE;
  $num = 0;

  while ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {
    $num = $num + 1;
    echo '<h1>';
    echo $row['title'];
    echo '</h1>';
    echo '<p><b>[ ';
    echo $row['date'];
    echo ' ] </b></p><p>';
    $content = substr($row['content'],0,200);
    echo $content;
    echo '</p>';
    echo $row['links'];
    echo '<p><b><a href="journal.php?view="';
    echo $row['id'];
    echo '">View Comments</a></b></p>';

    $first = FALSE;
  }

  if ($first) {
    echo 'Nothing here.';
  }
  exit;

} else {

if(isset($_POST['submit'])) {
  $n = $_POST['name'];
  $c = $_POST['comment'];

  $query = "INSERT INTO journalComment (name, comment, date, journalid) VALUES ('$n', '$c', NOW(), '$p_id')";
  $result = @mysql_query($query);
}

  $query = "SELECT title, content FROM journal WHERE id = $p_id";
  $result = @mysql_query($query);
  $row = @mysql_fetch_array($result, MYSQL_ASSOC);
  echo '<h1>';
  echo $row['title'];
  echo '</h1>';
  echo '<p><b>[ ';
  echo $row['date'];
  echo ' ] </b></p><p>';
  echo $row['content'];
  echo '</p>';

  $query = "SELECT name, comment, DATE_FORMAT(date, '%M %d, %Y') AS date FROM journalComment WHERE journalid = $p_id ORDER BY id DESC"; 
  $result = @mysql_query($query);

  echo '<h1>Comments</h1>';
  while($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<p>';
    echo '<i>';
    echo $row['date'];
    echo '</i> - ';
    echo '<b>';
    echo $row['name'];
    echo '</b></p><p>';
    echo $row['comment'];
    echo '</p>';
  }

  ?>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <h2>Add a Comment</h2>
  <p><b>Name</b><br><input type="text" name="name" size="25"></p>
  <p><b>Comment</b><br><textarea rows="8" cols="20"></textarea></p>
  <div align="center"><input type="submit" name="submit" value="Post"></div>
  </form>
  <?php  

} 
?>

</div>

<?php
  include_once('footer.html');
?>


To fix your comment box problem, instead of doing this:
}

  ?>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <h2>Add a Comment</h2>
  <p><b>Name</b><br><input type="text" name="name" size="25"></p>
  <p><b>Comment</b><br><textarea rows="8" cols="20"></textarea></p>
  <div align="center"><input type="submit" name="submit" value="Post"></div>
  </form>
  <?php  

} 
?>


Do this:
}
  
  echo '
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <h2>Add a Comment</h2>
  <p><b>Name</b><br><input type="text" name="name" size="25"></p>
  <p><b>Comment</b><br><textarea rows="8" cols="20"></textarea></p>
  <div align="center"><input type="submit" name="submit" value="Post"></div>
  </form>  
  ';

} 
?>


That didn't fix anything:
http://www.noenemies.com/journal.php

And I still have the problem of it not showing any information from the database...

Maybe $p_id=$_REQUEST['view'] so it's not doing the databse stuff, but it is doing the comment stuff? I don't know what $_REQUEST['view'] actually equals....

Wallows, I just visited your website and sent you an email but it was bounced back to me undeliverable.

$_REQUEST['view'] sees what is after the ".php" in the URL so like, if the URL was journal.php?view=555 then $_REQUEST['view'] would equal 555.

I set the code so if $_REQUEST['view'] equals nothing, it would show the index of all the journal entries, and if it equals something, it would show the journal entry with that ID. But for some reason, it doesn't work that well... :P

Ah, so $_REQUEST is like $_GET.
Well, it beats me, Viperfish sounds like he got something though.

@Viperfish - Was it your affiliate email? I got that. My host is really messed up, and I hate them. Emails always bounce back, but I always get them anyway. If it wasn't about affiliation, then I didn't get it...I only got two emails about affiliates.