Archive

Archive for the ‘Frameworks and Libraries’ Category

How to get the exact number of characters in a string, if it has a different encoding

June 25th, 2009 wiley No comments

How to get the exact number of characters in a string, if it has a different encoding.
In my php – QCubed project i need to get a exact length of substring from a string. To get a title from a whole string. In the standard way may use php function substr.

<?php echo substr(QApplication::Translate('_NEWS_TEXT1_'), 0, 45) . ' ...'; ?>

Where QApplication::Translate(‘_NEWS_TEXT1_’) is whole string get from a i18n implementation on QCubed. The problem is that string is a UTF-8 encoding, and in different language encoding function substr return different length of substring. The problem is that in UTF-8 encoding different characters have different numbers of bytes.
eg.

echo strlen('здрасти');

This code return 14 instead of expected 7. In other way:

echo strlen('zdrasti');

return exactly 7.
Тhis is so because the first string ‘здрасти’ is in UTF-8 Bulgarian language, where each character is different bytes length, and the second string ‘zdrasti’ is with one byte for one character.

Тo enable the first code example to show 45 characters, it is necessary to use that rate to increase this number to receive the exact number of bytes to 45 displayed characters.

$kUtfString = strlen(QApplication::Translate('_NEWS_TEXT1_')) / mb_strlen(QApplication::Translate('_NEWS_TEXT1_'), 'UTF-8');

The function mb_strlen return the real number of chars in string.

Solving the above problem that happens when the first change function as follows:

<?php echo substr(QApplication::Translate('_NEWS_TEXT1_'), 0, 45*$kUtfString) . ' ...';?>

Unique visitors to post: 2

Categories: PHP, QCube Tags: , ,

Read entry from directory handle

June 21st, 2009 wiley No comments

Howto read entry from directory handle.
In my QCube project i use php readdir function to read all i18n languages files.
Foreach language file, i create one button with label of current language and an Action to change current language on my site.

// function to check all i18n language files
protected function  agregateLanguage(){
$i = 0;
if ($handle = opendir(__QCUBED__ . '/i18n')){
while (false !== ($file = readdir($handle))){
$filearr = explode('.',$file);
if (end($filearr) === 'po'){
$this->btnLang[$i] = new QButton($this);
$this->btnLang[$i]->Text = $filearr[0];
$this->btnLang[$i]->ActionParameter = $filearr[0];
$this->btnLang[$i++]->AddAction(new QClickEvent(), new  QAjaxAction('btnLang_Click'));
}
}
}
}

// Function to change current language on my site
public function btnLang_Click($strFormId, $strControlId, $strParameter){
$_SESSION['language_code'] = $strParameter;
QApplication::Redirect('');
}

This code is used QCube my project but can be used with some changes for other php scripts.


Unique visitors to post: 2

Categories: PHP, QCube Tags: , ,

QCodo JavaScriptAction

May 11th, 2009 wiley No comments

I propose a solution which applied in my draft QCodo. When clicking with the mouse in the TextBox is caused QJavaScriptAction event, which performs a separate javascript function. Function to clear the text content in TextBox.

This is the code in a php script QCodo:


//........................

$this->txtSearch = new QTextBox($this);
$this->txtSearch->Width = '160px';
$this->txtSearch->Text = 'претърсване ...';
$str = $this->txtSearch->ControlId;
$this->txtSearch->AddAction(new QClickEvent(), new QJavaScriptAction("txtSearchClick('$str');"));

//.......................

Row 6 is located in function QCodo, which handles event on click.

This is the javascript code in HTML portion:


<script type="text/javascript">
function txtSearchClick(str) {
document.getElementById(str).value = '';
}
</script>

Where the variable str is sent from PHP scripts. str variable contains a TextBox ControlId. It is necessary to allow the script to recognize Control, whose text removed.

For me it worked without a problem.
I use it in the Search Box on the following page:
http://avalonbg.com/products


Unique visitors to post: 0

Categories: QCodo Tags: , ,
Google Analytics integration offered by Wordpress Google Analytics Plugin