The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 2 of 2

Thread: test

  1. #1

    truncate a string

    A paragraph can be truncated in php by following procedures:

    Call a function -> truncate_string($string, length);

    PHP Code:
    function truncate_string($data$max)
        {
    //String truncated by '.'
          
    $val=explode('.',$data);
          if(
    count($val)>1)
          {
          
    $max=2;
          
    $re "^s*[^.?!]+[.?!]+s*";
            
    $out "";
            for(
    $i 0$i $max$i++) {
                if(
    ereg($re$data$match)) {
                    
    //if a sentence is found, take it out of $data and add it to $out
                    
    $out .= $match[0];
                    
    $data ereg_replace($re""$data);
                }
                else {
                    
    $i $max;
                }
            }
            
            return 
    $out;
        
            }else
            {
      
    //truncate by words, call another function 
             
    $word=truncate_words($data);
             return 
    $word;
            }
            
        }
    // function for truncate by words     
        
    function truncate_words($data)
        {
          
    $str=preg_replace('/s+?(S+)?$/'''substr($data0,200));
          return 
    $str;
        } 
    have a nice teaser of long string body.
    Last edited by subhash.purohit; July 27th, 2009 at 02:22 AM.

  2. #2
    Do you have a working example of this somewhere? im not sure what you mean by: 'have a nice teaser of long string body'

    If you mean that whenever you look at the source code, you only see one line of code... i'd say thats pretty useless to do... since most editors and firebug can reformat the code to make it readable again... (and most people that want to look at the code to actually do something with it have an editor and/or firebug installed)
    Last edited by RvGaTe; July 24th, 2009 at 06:28 AM. Reason: typo

    You can never underestimate the stupidity of the general public.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012