PHP

Word count like function Ver.1.0! This function can count your input text

PHP Comments Off
Word count like function Ver.1.0! This function can count your input text
and display only part of it (like a description) without cutting words in half

  function smart_text ($text_input_var[string] ,text_length[int])
  {
      $strip_content=stripslashes($content);
      $all_content=strlen("$strip_content"); 

      $standard_content=substr($strip_content ,$text_length);
      $compare=stristr($standard_content ," ");
      $minus_content=strlen("$compare"); 

      $result_content=$all_content-$minus_content;
      $display_content=substr($strip_content ,0, $result_content); 

      $striped_content=stripslashes($display_content);
      echo nl2br($striped_content);
  } 

For example i insert a 300 words test but only want about 3 rows of it to be displayed
without cutting words in half. 

function smart_text($text,200) 

Displayed result will be the first 200 letters of the text plus the letters till the first space
char.

Trim Function

PHP Comments Off
Trim Function

Left - ltrim() function
<?php 

$text = "\t\tThese are a few words :)  ...  ";
$trimmed = ltrim($text);
// $trimmed = "These are a few words :)  ...  "
$trimmed = ltrim($text," \t.");
// $trimmed = "These are a few words :)  ...  "
$clean = ltrim($binary,"x00..x1F");
// trim the ASCII control characters at the beginning of $binary
// (from 0 to 31 inclusive) 

?> 

Right - rtrim() function
<?php 

$text = "\t\tThese are a few words :)  ...  ";
$trimmed = rtrim($text);
// $trimmed = "\t\tThese are a few words :)  ..."
$trimmed = rtrim($text," \t.");
// $trimmed = "\t\tThese are a few words :) "
$clean = rtrim($binary,"x00..x1F");
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive) 

?>

This function allows you to take any string and split it into fairly even pieces

PHP Comments Off
This function allows you to take any string and split it into fairly even pieces

<? 

function breakString($str="", $pieces="") { 

// Find out how big the string is
$len = strlen($str); 

// String size can't be less than pieces
if ($len < $pieces) {
    $ary[0] = "String Size is less than Number of Pieces";
    return $ary;
} 

// Find out how big each piece should be
$lim = $len / $pieces;
$start = 0;
$i = 0;
$l = 0; 

// Split string into individual characters
for ($k = 0; $k < $len; $k++) {
     $x = $k + 1;
     $g[$k] = substr($str,$k,1);
} 

// Start Making Pieces
while ($i < $pieces) { 

// Glue Pieces Back Togeather
   for ($j = $start; $j < $lim; $j++) {
       $tmp = $tmp . $g[$l];
       $l++;
   } 

// Back up one character in the array
   $x = $l - 1; 

// If Last Charecter is NOT a space, AND this is NOT the last piece,
// Go until the next  space is found
   while (($g[$x] != " ") AND ($i != $pieces -1)) {
     $tmp = $tmp . $g[$l];
     $g[$x] = $g[$l];
     $l++;
   } 

// Move this String into array piece
   $ary[$i] = $tmp;
// Reset String
   $tmp = "";
   $i++;
} 

// Output String
   return $ary; 

} 

$text = "this is a little test with php3, MYSQL and Apache on my webserver. It works fine but i have to learn a lot about php3!";
echo("<b>$text</b><br><br>"); 

$now = breakString($text, 3); 

$sze = sizeof($now); 

for ($ii = 0; $ii < $sze; $ii++) {
     echo("$now[$ii]<br>");
} 

?>

Sub String Replace

PHP Comments Off
Sub String Replace

<?php
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr>\n"; 

/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br>\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br>\n"; 

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br>\n"; 

/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br>\n";
echo substr_replace($var, 'bob', -7, -1) . "<br>\n"; 

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br>\n";
?>

Sorting Strings

PHP Comments Off
Sorting Strings

<?php
$arr1 = $arr2 = array("img12.png","img10.png","img2.png","img1.png");
echo "Standard string comparison\n";
usort($arr1,"strcmp");
print_r($arr1);
echo "\nNatural order string comparison\n";
usort($arr2,"strnatcmp");
print_r($arr2);
?> 

/* Output
Standard string comparison
Array
(
    [0] => img1.png
    [1] => img10.png
    [2] => img12.png
    [3] => img2.png
) 

Natural order string comparison
Array
(
    [0] => img1.png
    [1] => img2.png
    [2] => img10.png
    [3] => img12.png
)
*/