NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Continued Strings - PHP Function

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 01-05-2007, 09:08 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!

Cool Continued Strings - PHP Function


Hey,

I made this function and posted it at my blog, so, i thought i may aswell post it here too.

This is a simple functions to cut strings down to "My lastest project...", from " "My lastest project is a CMS".

Great for "Last Post" colums, descriptions, titles etc....

The code
PHP Code:
<?php
/*************************************
/ Function Name: Continued Strings
/ Function Description: This cuts a string, and adds a pattern to the end. (User defined patterns, and length)
/ Author: Adrian
/ Website: Dreamit.co.uk
*************************************/

function Cont_String($string$length$pattern){

    if(
strlen($string) > $length){

    
$final substr("" $string ""0$length);
    
        
$final .= "" $pattern "";

    return 
$final;

    } else {

        return 
$string;

    }

}    

$about "Hello, my name is Adrian and i own Dreamit.co.uk";
????: NamePros.com http://www.namepros.com/code/277330-continued-strings-php-function.html
????: NamePros.com http://www.namepros.com/showthread.php?t=277330

    
$display Cont_String($about21"...");

echo 
$display;

?>
You can choose what "pattern" to display, most common one is "...", remember, when adding the pattern into the function surround the pattern with " ", otherwise you could get errors.

If you decide to cut strings at 21 characters, if the string is below that, no pattern will be added.

I hope you find this useful when adding it into your scripts and projects, it's a general thing that gets left out alof of scripts alot of the time, but is very useful.

Adrian
Hitch is offline  
Old 01-05-2007, 09:21 AM   #2 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
Pretty useful Hitch, but it could be cut down a bit
????: NamePros.com http://www.namepros.com/showthread.php?t=277330

PHP Code:
function Cont_String($string$length$pattern='...' /* default pattern is ellipse */ ){

    if(
strlen($string) > $length)
    {
        
$string substr($string0$length) . $pattern;
    }
    
    return 
$string;

Does exactly the same thing only it's a bit more efficient. (removed unneeded quotes, string manipulation is now 1 line and it now modifies the first functiona rgument instead of creating a new one so only need for one return [although return ends the function regardless so not technically needed])

Also the default pattern is an ellipse (...) if one is not supplied like you say in this line:
Quote:
remember, when adding the pattern into the function surround the pattern with " ", otherwise you could get errors.
It just makes sure that if they do not miss the third argument that it will use the default.

Just though I'd add that
Last edited by Matthew.; 01-05-2007 at 09:42 AM.
Matthew. is offline  
Old 01-05-2007, 09:54 AM   #3 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Using "" with nothing inside of it does nothing, btw.
Dan is offline  
Old 01-05-2007, 11:20 AM   #4 (permalink)
NamePros Regular
 
beaver6813's Avatar
Join Date: May 2005
Location: England
Posts: 390
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
 




Nice

I found this one on php.net a while back and have used it ever since whenever i need something truncated

PHP Code:
function truncate_string($string$max_length){

    if (
strlen($string) > $max_length) {

         
$string substr($string,0,$max_length);

         
$string .= '...';
????: NamePros.com http://www.namepros.com/showthread.php?t=277330

    }
    return 
$string;


Slightly less options for the suffix, but works fine for me
beaver6813 is offline  
Old 01-05-2007, 11:24 AM THREAD STARTER               #5 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!
Wow, didn't know that was on PHP.net

Looks like they call it "truncate_string", i had to make "Continued Strings" up.
Hitch is offline  
Old 01-05-2007, 11:25 AM   #6 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
beaver6813, that's the same thing.. just replace "..." with what you want.

I just searched php.net and found this. It makes sure it doesn't cut words apart.
Last edited by Dan; 01-05-2007 at 11:28 AM.
Dan is offline  
Old 01-05-2007, 12:34 PM   #7 (permalink)
NamePros Regular
 
beaver6813's Avatar
Join Date: May 2005
Location: England
Posts: 390
beaver6813 is a jewel in the roughbeaver6813 is a jewel in the roughbeaver6813 is a jewel in the rough
 




Originally Posted by Dan
beaver6813, that's the same thing.. just replace "..." with what you want.

I just searched php.net and found this. It makes sure it doesn't cut words apart.
I used either that snippet or another i found on php.net on the Readme Generator i made to limit the lines of the content to make sure it wasn't too long as notepad doesn't automatically terminate lines itself after so many words

And yeah i didn't check whether it was the same, i was just posting the one that i use in my scripts. Whilst we're at it posting examples of terminated lines, this one i used in my readme generator terminates the lines and writes the end product into a text file,
PHP Code:
function preserve_wordwrap($tstr$filename$len 80$br '
????: NamePros.com http://www.namepros.com/showthread.php?t=277330
'
) {
????: NamePros.com http://www.namepros.com/showthread.php?t=277330
     
$strs explode($br,$tstr);
     
$retstr "";
     foreach (
$strs as $str) {
         
$retstr .= wordwrap($str,$len,$br) . $br;
     }
$somecontent $retstr;

   if (!
$handle fopen($filename'a')) {
         echo 
"Cannot open file ($filename)";
         exit;
   }


   if (
fwrite($handle$somecontent) === FALSE) {
       echo 
"Cannot write to file ($filename)";
       exit;
   }
   
fclose($handle);
   return 
$retstr;

But anyways, Hitch bravo on not being lazy and making your own function
Last edited by beaver6813; 01-05-2007 at 12:38 PM.
beaver6813 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:21 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger