Dynadot โ€” .com Registration $8.99

Continued Strings - PHP Function

Spacemail by SpaceshipSpacemail by Spaceship
Watch

Hitch

Established Member
Impact
74
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:
<?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";

	$display = Cont_String($about, 21, "...");

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. :tu:

Adrian
 
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Pretty useful Hitch, but it could be cut down a bit :)

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

    if(strlen($string) > $length)
	{
		$string = substr($string, 0, $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:
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:
0
•••
Using "" with nothing inside of it does nothing, btw.
 
0
•••
Nice :)

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

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

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

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

         $string .= '...';

    }
	return $string;

}

Slightly less options for the suffix, but works fine for me :)
 
0
•••
Wow, didn't know that was on PHP.net :lol:

Looks like they call it "truncate_string", i had to make "Continued Strings" up. :lol:
 
0
•••
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:
0
•••
Dan said:
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.

:P 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:
function preserve_wordwrap($tstr, $filename, $len = 80, $br = '
') {
     $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 :great:
 
Last edited:
0
•••
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back