<?php
function generatePassword($intNumOfChars) {
if (is_numeric($intNumOfChars) && ($intNumOfChars > 0)) {
$strChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
for ($i = 0; $i < $intNumOfChars; $i ++) {
$strPassword .= $strChars[rand(0, strlen($strChars)-1)];
}
}
return $strPassword;
}
?>
PHP – Generate a random password based on length
PHP – Truncate a string to a given length
This is a function I use to display the ellipses when I want to truncate and display a summary of a larger body of text.
<?php
function truncateString($intLength = 0, $strText = "") {
if ($intLength == 0) {
return $strText;
}
if(strlen($strText) > $intLength) {
preg_match("/[a-zA-Z0-9]{0, " . $intLength . "}/", $strText, $strNewText);
return ($strNewText . "...");
}
else {
return $strText;
}
}
?>