We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Validation string length in Arabic

Hello :) I am using the following code to validate string length

$this->add('first_name', new StringLength(array(
        'max' => 15,
        'min' => 2,
        'messageMaximum' => 'The first name is too long',
        'messageMinimum' => 'The first name is required'
)));

it works perfectly when string is written in English, but when it comes to Arabic, the messageMaximum appears if I typed 8 letters



26.3k
edited Oct '14

Hi! perform a phpinfo() script, do you have mbstring extension installed and enabled?

The problem is that in UTF-8 (I am guessing that you are using it) in some situations to encode 1 sign you need more than 1 byte. There are so many signs world wide like latin, arabic, chinese, japanese, hebrew, cyrillic, math, chemical, phisics signs, etc. that 8 bits are not enough to encode everything. That is why sometimes (problalby with arabic letters) in UTF-8 a sign is encoded in more than one succeeding bytes. It could be e.g. 3 bytes. That is why when you type an SMS with a special letter (non english e.g. ź, š) you can type only 70 signs in 1 message instead of 160.

Example: كونراد has 6 letters but it is encoded in 12 bytes.

So the strlen() function in PHP does not count number of signs but it counts number of bytes. That's why strlen() will count 6. You need to use a function mb_strlen from mbstring extension. mb_strlen() function will count 12.

I have found now that Phalcon is checking if mb_strlen() function is available. See here. If it's not it will use simple strlen().

So I am guessing that you should install/enable the mbstring extension and mb_strlen() function will be available.

edited Oct '14

Thank you Conardaek!!

This is my phpinfo() result, the Multibyte language support is enabled! I am so happy that PhalconPHP is looking for mb_strlen() first, this is what expected from this cool framework ;)

the function_exists( ' mb_strlen ' ) returned true but my problem has not been solved :/



26.3k

Heh, so I think you need help of sb more experienced than just me:)

I have checked how it works on my machine with arabic letters and it works fine. I have Phalcon 1.3.3 with PHP 5.6.0 under Ubuntu.

1 What machine do you have?

2 You can try to check if mb_strlen() works fine, e.g.


var_dump(mb_strlen($_POST['first_name']));

Maybe this way you will find out where the problem is - with Phalcon or not.

PHP Version: 5.5.12 Server: WAMP Apache/2.4.9 (Win64)

the mb_strlen() did not work fine :/



26.3k
edited Oct '14

1 Which encoding do you use? Is it UTF-8?

Let's say that you use UTF-8, so then:

2 Did you defined UTF-8 in all HTML code? I mean a metatag?

3 Have you been carefull when creating all your files with your text editor? I mean did you saved all your files (models, vievers, controllers, library classes etc) in UTF-8 BOM?

If both 2 and 3 are "YES" so then:

4 Did you set encoding of Phalcon's response to UTF-8? You can do it when declaring services, there is a method setContentType() in a class Phalcon\Http\Response. To be honest, I don't do it now and it works for me but maybe this is an issue by you.



11.2k
Accepted
answer

Use the following function in the beginning of your bootstrap file:

mb_internal_encoding('UTF-8');

And check if your text editor is saving your files in UTF-8

it works ^_^ The mb_internalencoding('UTF-8'); solve it ^^

Thank you Conradaek, Thank you heptagono ^_^