Categories

Highlight a string

<?php
$msg1 = “<?php echo \”test message to display\”;
highlight_string($msg1)
?>

Percentage Of Similar Text Between 2 Strings

This is a useful little fiunction you may not have heard of called similar_text.

This function will take 2 strings and compare then and return the percentage of the text that is the same as a float.

<?php

$text1 = “Welcome to getphp , we hope you enjoy our content”;
$text2 = “Hello this is getphp, we hope you [...]

replace tabs in a string

<?php
$original = “string\tstring\tstring\tstring\t”;
//replace the tabs with a space
$replaced = str_replace(“\t”, ‘ ‘, $original);
echo “<pre>{$original}</pre>”;
echo “<pre>{$replaced}</pre>”;
?>

Check the amount of characters in a string function

<?php
//check the amount of characters in a string function
Function CheckNoChars($strText)
{
//check for between 6 and 12 characters
if (eregi(“^.{6,12}$” , $strText))
return true;
else
return false;
}
?>
<?php
//test the function
$str1 = “mypasswordistoolong”;
if (CheckNoChars($str1))
//if its OK display this message
echo “this has the correct number of characters”;
//if its not OK display this one instead
else
echo “incorrect number of characters”;
?>

Using trim to get rid of whitespace

This shows how to use the trim function to remove white spaces from either side of a string

<?php
//trimming white space from a string
//our two strings
$string1 = ” spaces before”;
$string2 = ” spaces before and after “;
//remove the whitespace
$trimmed_string1 = trim($string1);
$trimmed_string2 = trim($string2);
//display the text
echo $trimmed_string1;
echo “<br>”;
echo $trimmed_string2;
?>

Encrypt a password

Encrypt a password in PHP

<?php
$password = “abc123″;
echo($password.”<br>”);
$salt = “getphp”;
echo(crypt($password, $salt));
?>