Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save function is cropping strings - laravel

Tags:

php

laravel

I am saving some strings into database (mysql), but laravel always saves this for example "Dru" instead of "Društvo". How can I resolve this? Its cropping string at point where is character with caron. If I manually save string and then fetch it laravel correctly displays it but if I write same string in view file I just get �. Any solutions?

like image 623
Tim Avatar asked Feb 15 '26 07:02

Tim


1 Answers

  1. Make sure the database charset/coallition is UTF-8.
  2. On the page you insert these russian characters ( the form, textarea ), make sure the encoding is UTF-8, by setting Content-Type to text/html; charset=utf-8. Enter in russian text directly to the form input.
  3. On the processing page that handles this form, which inserts it into the database, make sure to do SET NAMES utf8 so it's stored as UTF-8 before you insert the data, in a separate query beforehand.
  4. When you render the content from the database in a view, make sure the Content-Type is text/html; charset=utf-8.

Make sure that the content-type is not windows-1251 or iso-8859-1/latin1. Make sure the database charset/coallition is NOT ISO-8859-1/Latin1.

Second way to store character in mysql:

Try calling mysql_set_charset('utf8'); after connecting to the database. I think it's similar to executing a SET NAMES query, but since the PHP manual says using that function over a SET NAMES query is recommended, I'd try it.

At last, Also, when you display your content, you could try Also, when you display your content, you could try echo htmlentities($string, ENT_COMPAT, 'UTF-8');

I had same problem in laravel but I figure out this way.let me know if any query.

like image 189
Jigs Virani Avatar answered Feb 16 '26 21:02

Jigs Virani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!