Using str_split on a UTF-8 encoded string

前端 未结 7 1548
天命终不由人
天命终不由人 2020-11-29 00:37

I\'m currently working on a project, and instead of using regular MySQL queries I thought I\'d go ahead and learn how to use PDO.

I have a table called contestants,

相关标签:
7条回答
  • 2020-11-29 00:43

    The str_split function splits by byte, not by character. You'll need mb_split.

    0 讨论(0)
  • 2020-11-29 00:46

    str_split does not work with multi-byte characters, it will only return the first byte - thus invalidating your characters. you could use mb_split.

    0 讨论(0)
  • 2020-11-29 00:49

    Mind that the utf8 declaration used in your connect-string is reported to be not working. In the comments on php.net I frequently see this alternative:

    $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass,
                        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
    
    0 讨论(0)
  • 2020-11-29 01:02

    UTF-8 PROBLEMS & SOLUTIONS by PHP FUNCTIONS

    1. How to Save UTF-8 Charterers (mathematical string,special chars like 92 ÷ 8 ÷ 2 = ? ) ?

    Ans. $string =utf8_encode('92 ÷ 8 ÷ 2 = ?');

    2. How to print UTF-8 Charterers From Database ?

    Ans. echo utf8_decode($string);

    Note: If you do not want to do this by using encoding/decoding you can do this via.

    1. if you are using mysqli_query() then

    $conn = mysqli_connect('localhost','db_username','password','your_database_name');
    mysqli_set_charset($conn,"utf8"); 
    

    2.If you are using PDO then

    class Database extends PDO{
        function __construct() {
            parent::__construct("mysql:host=localhost;dbname=your_db_name","gurutslz_root","Your_db_password",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        }
    }
    $conn=new Database();
    
    0 讨论(0)
  • 2020-11-29 01:06

    this work for me... hope its usefull.

    ensure that the database, apache and every config was in utf8.

    PDO OBJECT

                $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . config::read('db.basename') .';charset=utf8'. ';port=' . Config::read('db.port') .';connect_timeout=15';
                $user = Config::read('db.user');
                $password = Config::read('db.password');
                $this->dbh = new PDO($dsn, $user, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
                $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    

    it work if not using another function like str_word_count.

    USING str_word_count you need to use utf8_decode(utf8_encode)..

    function cortar($str)
    {
        if (20>$count=str_word_count($str)) {
            return $str;
        }
        else
        {
            $array = str_word_count($str,1,'.,-0123456789()+=?¿!"<>*ñÑáéíóúÁÉÍÓÚ@|/%$#¡');
            $s='';
            $c=0;
            foreach ($array as $e) {
                if (20>$c) {
                    if (19>$c) {
                    $s.=$e.' ';
                    }
                    else
                    {
                    $s.=$e;
                    }               
                }
                $c+=1;
            }
            return utf8_decode(utf8_encode($s));
        }
    }
    

    function returs string with 20 words.

    0 讨论(0)
  • 2020-11-29 01:07

    UTF-8 Using PDO

    problems when writing international (even Chinese and Thailandic) characters to the database

    there may be more ways to make this work. I am not an expert, just a tech-freak, interested to understand all this. In Linux and Windows I have set up a few CMS (content-managing-systems), using a sample from the following website:

    'http://www.elated.com/articles/cms-in-an-afternoon-php-mysql'

    The sample is using PDO for insert, update and delete.

    It took me a few hours to find a solution. Whatever I did, I always concluded differences between the data in my forms and in the phpmyadmin/heidi -views

    I followed the hints of: 'https://mathiasbynens.be/notes/mysql-utf8mb4' but there was still no success

    In my CMS-structure there is a file 'Config.php': After reading this webpage I changed the line

        define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings);
    

    to

        define( 'DB_DSN', 'mysql:host=localhost;dbname=mythings;charset=utf8');
    

    Now all works fine.

    0 讨论(0)
提交回复
热议问题