Top

Count repeats of words/letters in a paragraph

September 30, 2007

This snippet counts how many words or letters there are in a paragraph/line of text.

<?php

$string = "Hello world!"; // Set the source of string, could be $_GET, $_POST etc as well.
$splitby = "chars"; // Set to "words" or to "chars"

$string = stripslashes($string);
$string = strtolower($string); // Makes H and h the same.

if($splitby == "words") {

$array = preg_split("/ /",$string);

} else {

$array = preg_split("//",$string);

}

foreach($array as $a => $k) {

$arr[$k] = $arr[$k] + 1;

}

foreach($arr as $a => $k) {

if($a == " ") { $a = "[space]"; }
if($a == "") { unset($a,$k); }

if($a != "" && $k != "") {

$a = htmlentities($a);
$k = htmlentities($k);

echo "$a - $k
\n"
;

}

}

?>

Demo at http://danltn.com/test/split.php - It’s doing Hello World and splitting per character. :)

Remove all duplicate words

September 19, 2007

Hi there, my latest script allows the user to remove all duplicate words entered into a text box.

A demo is here: http://danltn.com/test/uniqueexplode.php

Source is here: http://danltn.com/test/uniqueexplode.phps

Here is a test string you can use to test it:

This string can be used to test the script, this string is very effective at doing its job, as it has repeated words such as “the at as”.

Bonuses of this are it doesn’t allow server side usage, and it also keeps all punctuation in the sentence.

Show X amount of random links without duplicates PHP script

September 10, 2007

Hi there, my latest snippet allows you to do the following:

Shows an administrator defined amount of links, easily added and modifiable, it is written in PHP not JavaScript so it is much more compatible. It also ensures there are no duplicate links shown, so the same user doesn’t see, for example 4 Google.com’s and 1 Yahoo.com

Code available here: CODE

Demo available here: DEMO

Enjoy it!

Bottom