Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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