php


Update: ElStr.class.php goes to v1.2

php-iconThe PHP Class ElStr with usefull functions for modern greek unicode text manipulation, such as transcript greek to latin, or accent marks stripping is now updated to version 1.2

What’s new?

  • mixed case support for difthongs, etc:
    • μπ => mp or τζ => tz
    • Μπ => Mp or Τζ => Tz
    • μΠ => mP or τΖ => tZ
    • ΜΠ => MP or ΤΖ => TZ

  • Two new accent mark removal methods:
    • strtolower_no_accent($str) – that Convert unicode string to lower case, without accent marks for the greek letters
    • str_no_accent($str) – that Remove accent marks for the greek letters at passed unicode string (no any case convertion)

Examples

require('ElStr.class.php') ;
$txt = "Το μπαρμπουνάκι θέλει μπυρίτσα" ;
$elstrObj = new El_Str() ;

echo $elstrObj->to_latin($txt) ;
//echoes: To barmpounaki thelei byritsa

echo $elstrObj->strtoupper_no_accent($txt) ;
//echoes: ΤΟ ΜΠΑΡΜΠΟΥΝΑΚΙ ΘΕΛΕΙ ΜΠΥΡΙΤΣΑ

echo $elstrObj->strtolower_no_accent($txt) ;
//echoes: το μπαρμπουνακι θελει μπυριτσα

echo $elstrObj->str_no_accent($txt) ;
//echoes: Το μπαρμπουνακι θελει μπυριτσα

Downloads

Download v1.2      View Source


ElStr.class.php

php-iconA PHP Class with usefull functions for modern greek unicode text manipulation, such as transcript greek to latin, or upper case greek letters stripped from accent marks.

Functions
string to_latin($str)

  • Convert greek letters at the string to latins, as ISO:843 / ΕΛΟΤ:743 defines
  • $str string to convert
  • Returns converted string

string strtoupper_no_accent($str)

  • Convert unicode string to upper case, without accent marks for the greek letters
  • $str string to upper case convert
  • Returns converted string

bool is_upper($char, $notGreekException = false)

  • Check if $char is upper case
  • $char is the character for checking
  • If $notGreekException == true, throws exception when char is not greek
  • Returns true if $char is upper case, else false

bool is_lower($char, $notGreekException = false)

  • Check if $char is lower case
  • $char is the character for checking
  • If $notGreekException == true, throws exception when char is not greek
  • Returns true if $char is lower case, else false

Download      Frok it at Github

License
This project is free software, distributed under the terms & conditions of the FreeBSD License.


PHP random password generator

Here is a function for random password generation, could be useful in a lot of PHP projects.


/**
* @author Petros Kyladitis - 2012
* @license MIT License
*
* @description random password generator
* @param $min_length the minimum size of the password
* @param $length the maximum size of the password
* @param $chars a string contains all acceptable password characters
* @return a string with the generated password
*/

function rand_pass($min_length, $max_length=null, $chars="qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"){
  //if $max_length param is not set, pass $length is the $min_length 
  //param, else a random number between min and max params.
  $length = !isset($max_length) ? $min_length : rand($min_length, $max_length) ;
  
  //the position of the last character of $chars variable
  $chars_end = strlen($chars) - 1;
  
  //variable for store password
  $pass ;
  
  //select a random char from the $chars & concatenate to the $pass
  for($i=0; $i<$length; $i++)
    $pass .= substr($chars, rand(0, $chars_end), 1) ;
  
  return $pass ;
}

Unexpected PHP session_start() error

When you write PHP code with sessions, and without any apparent reason you got a message like

ERROR Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by…

and you are sure that the session_start() function is before any other code, or you don’t have made any other mistake, don’t start checking your code or make any changes at it.

Maybe it’s a problem with the file encoding. Using session_start() function, in a unicode encoded file mainly at MS Windows throws the above error, because many Windows programs using Unicode with BOM and add the bytes 0xEF, 0xBB, 0xBF at the start of any document. So, this sends the standard html header before the cookie and makes this the php statement invalid.

The solution is to change the file encoding to Unicode WITHOUT BOM. If you’re using Notepad++, just go the Encoding menu and make the appropriate selection, as shown in the image.


Javascript Function: strToPhpChr(str)

There are times when converting a string into PHP concatenated chr( ) functions are very useful.
So, here is a simple javascript function, who do the dirty job.

/**
* @author Petros Kyladitis - 2011
* @license MIT License
* @description get PHP concatenated chr( ) functions from a string
* @param str String to be converted
* @return the PHP code
*/
function strToPhpChr(str){
  var strLen = str.length ;
  var phpCharCode = '' ;
  for(i=0; i<strLen; i++){
    phpCharCode +=  'chr(' + str.charCodeAt(i) +  ').' ; 
  } 
  return phpCharCode.substring(0, phpCharCode.length-1) ; 
}