displaying first 50 characters of a mysql text field?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser32423

    displaying first 50 characters of a mysql text field?

    Hi all

    I have been searching for hours on the php website for a function that
    will allow me to display just the first 50 chars of a mysql text field
    in a results table, but i'm having no luck. I need it to create a
    results page for a site i'm creating in php. Can anyone help with this
    problem?

    Kind regards

    Marc

  • David Haynes

    #2
    Re: displaying first 50 characters of a mysql text field?

    phpuser32423 wrote:[color=blue]
    > Hi all
    >
    > I have been searching for hours on the php website for a function that
    > will allow me to display just the first 50 chars of a mysql text field
    > in a results table, but i'm having no luck. I need it to create a
    > results page for a site i'm creating in php. Can anyone help with this
    > problem?
    >
    > Kind regards
    >
    > Marc
    >[/color]

    $str50 = substr($mystrin g, 0, 50);

    -david-

    Comment

    • Geoff Berrow

      #3
      Re: displaying first 50 characters of a mysql text field?

      I noticed that Message-ID: <0dB6f.4$jW4.3@ fe24.usenetserv er.com> from
      David Haynes contained the following:
      [color=blue]
      >
      >$str50 = substr($mystrin g, 0, 50);[/color]

      I use this method in a function and if the text is truncated the
      function adds '...more' at the end. The function also takes a url so
      that the word 'more' can be a link to an expanded version. $length is
      obviously the amount you to which you want to truncate. The function
      also 'steps back' to avoid truncating in the middle of a word.

      <?php
      function truncate($value ,$length,$url){
      if(strlen($valu e)>$length){
      $value=substr($ value,0,$length );
      $n=0;
      while(substr($v alue,-1)!=chr(32)){
      $n++;
      $value=substr($ value,0,$length-$n);
      }
      $value=$value." ...<a href='$url'><em >more</em></a>";
      }
      return $value;
      }
      //usage
      echo truncate("the quick brown fox jumps over the lazy
      dog",15,"http:w ww.example.com? id=someid");
      ?>
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • phpuser32423

        #4
        Re: displaying first 50 characters of a mysql text field?

        thanks guys thats a great help

        Comment

        • Craig Storey

          #5
          Re: displaying first 50 characters of a mysql text field?

          phpuser32423 wrote:[color=blue]
          > Hi all
          >
          > I have been searching for hours on the php website for a function that
          > will allow me to display just the first 50 chars of a mysql text field
          > in a results table, but i'm having no luck. I need it to create a
          > results page for a site i'm creating in php. Can anyone help with this
          > problem?
          >
          > Kind regards
          >
          > Marc
          >[/color]

          If you only need the first 50 chars for that field then you can get
          MySQL to do the work for you ...

          MySQL - Substring: Extracting part of a string

          SELECT name, SUBSTRING(name, 0,50) FROM cia WHERE region = 'Asia'

          MySQL docs... http://dev.mysql.com/doc/refman/5.0/...functions.html

          Craig


          Comment

          Working...