PDA

View Full Version : Code Snippets



Emancipator
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?

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

Emancipator
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.

r2d2
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 ;)

Emancipator
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.

Chris
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)

Emancipator
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.

Chris
06-29-2005, 02:35 PM
http://business.cch.com/help/Avoiding_noise_words_in_your_search.htm
http://meta.wikimedia.org/wiki/MySQL_4.0.20_stop_word_list

Emancipator
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.

Emancipator
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.

Chris
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.

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

Chris
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."

Emancipator
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
http://www.horror-movies.ca/ageofthedead/

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

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

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

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

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