View Full Version : Code Snippets

06-29-2005, 12:29 PM
I write alot of code snippets for my custom CMS system to improve it and am wondering if this is an ok board to share code snippets I write that do various usefull thinks. Replace words, shorten titles based on variables, etc. I use them in a mass inclusion file on my CMS systems.

It is helpfull for me since I have a place to go back and retrieve the code when I overwrite it with an update (which happens) and it may help other folks.

I would post the code, and an explanation of how to use it. Sound ok?

06-29-2005, 12:42 PM
sounds great to me.

06-29-2005, 12:54 PM
This is a sample of what im thinking

How to use: Cut and paste into the top of your page

function sho_tit($string, $len)
$oRIGINAL = $string;
$original2 = strlen($oRIGINAL);
if ($original2 > $len )
{ $string= substr_replace($oRIGINAL, '...', $len); }
if ($original2 <= $len - 1 )
{ $string = $string; }
return $string;

How to use this code: Using the example below will shorten the long title to only the first 5 characters. Great for making short titles. You specify the number, and it shortens it to the length you want..

$title = 'This Title is to big so I want to shorten it';
echo sho_tit($title, 5);

In my CMS system its in an inclusion file so that you dont have to call the function everytime you use it.

06-29-2005, 01:18 PM
Sounds like a good idea. Couldnt you do the above with substr (http://uk2.php.net/manual/en/function.substr.php) though? Maybe it was just an example?

Also a function called 'sho_tit' could be taken in a different way ;)

06-29-2005, 01:20 PM
yeah my abbreviation of Show Title was not intended to be something profane.. lol. There are many ways to do everything this is just my 4second method with some polish.

06-29-2005, 01:26 PM
I have a function for you... well I don't actually have it, but I think it'd be a good class to be plugged into CMS's.

Basically take an argument that is a text blob and parse out any common noise words (there are lists of noise words available online). Take the remaining words, count occurences, order them by number of occurences, then print a delimited list.

Basically insta-keywords for the page for whatever purpose needed (meta tags, internal site search, etc)

06-29-2005, 01:38 PM
I am actually working on such a function for another purpose. Give me a set of comma delimited noise words and i can modify it to do what you are asking very easily.

06-29-2005, 02:35 PM

06-29-2005, 05:06 PM
I have a site search built where I filter words, this list you found will be a nice addition to it.

06-29-2005, 06:00 PM
does anyone have any use for this strip-stop-words function once i have it complete? I am about half way done.

08-18-2005, 11:51 AM
You ever finish it?

I also thought of another cool snippet -- what about parsing a block of text for the second instance of </p><p> and then inserting an ad (square inline content ad). Basically automatically inserting an ad after the first paragraph.

08-18-2005, 01:28 PM
hey chris why not have a code snippet section on this site?

08-18-2005, 05:55 PM
What do you think this thread is ;) ?

Maybe Emancipator would like to write an article "Common PHP code snippets for content sites."

08-18-2005, 06:21 PM
sure i would be willing to do that.. sorry i have not posted any followups I have been working on a CRAPLOAD of new stuff. Where do I send the article chris? I may have some time tomorrow and be happy to share some of my code.

Ive been busy working on my game

Thats my XMLhttp game that uses php and has a dynamic game map. I have some potty humor on it, try and login, just make something up and watch the zombies :P

08-18-2005, 07:48 PM
Hahaha, I like it. *thumbs up*

08-18-2005, 08:31 PM
That is pretty funny. How about "Common PHP code snippets for php driven sites"

08-18-2005, 08:49 PM
I think "Common Code Snippets for PHP-Driven Sites" is a bit less redundant.

08-19-2005, 07:03 AM
just email it to editor @ this domain