1

I'm developing with PHP 7.0 and I have tried to use the function utf8_encode() and I have got the error "Call to undefined function utf8_encode()"

I have to try to install php7.0-xml module with sudo apt-get install php7.0-xml and I get this error:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 php7.0-xml : Depends: php7.0-common (= 7.0.18-0ubuntu0.17.04.1) 
but 7.0.18-1+deb.sury.org~yakkety+1 is to be installed
E: Unable to correct problems, you have held broken packages.

If I make sudo apt-cache search php7.0-* I've got this result:

libapache2-mod-php7.0 - lado servidor, lenguaje de guionización HTML-embebido (módulo Apache 2)
php7.0 - Lenguaje de guión empotrado para HTML en el lado del servidor (metapaquete)
php7.0-cgi - Lenguaje de guiones embebidos en HTML para servidores (binario CGI)
php-all-dev - package depending on all supported PHP development packages
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-common - documentation, examples and common module for PHP
php7.0-curl - CURL module for PHP
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
php7.0-odbc - ODBC module for PHP
php7.0-opcache - Zend OpCache module for PHP
php7.0-pgsql - PostgreSQL module for PHP
php7.0-pspell - pspell module for PHP
php7.0-readline - readline module for PHP
php7.0-recode - recode module for PHP
php7.0-snmp - SNMP module for PHP
php7.0-sqlite3 - SQLite3 module for PHP
php7.0-tidy - tidy module for PHP
php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.0-xmlrpc - XMLRPC-EPI module for PHP
php-geoip - GeoIP module for PHP
php-xdebug - Xdebug Module for PHP
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php-amqp - AMQP extension for PHP
php-apcu - APC User Cache for PHP
php-gearman - PHP wrapper to libgearman
php-gmagick - Provides a wrapper to the GraphicsMagick library
php-gnupg - PHP wrapper around the gpgme library
php-http - PECL HTTP module for PHP Extended HTTP Support
php-igbinary - igbinary PHP serializer
php-imagick - Provides a wrapper to the ImageMagick library
php-libsodium - PHP wrapper for the Sodium cryptographic library
php-mailparse - Email message manipulation for PHP
php-memcache - memcache extension module for PHP
php-memcached - memcached extension module for PHP, uses libmemcached
php-mongodb - MongoDB driver for PHP
php-msgpack - PHP extension for interfacing with MessagePack
php-oauth - OAuth 1.0 consumer and provider extension
php-pinba - Pinba module for PHP
php-propro - propro module for PHP
php-ps - ps module for PHP
php-radius - radius client library for PHP
php-raphf - raphf module for PHP
php-redis - PHP extension for interfacing with Redis
php-remctl - PECL module for Kerberos-authenticated command execution
php-rrd - PHP bindings to rrd tool system
php-smbclient - PHP wrapper for libsmbclient
php-solr - PHP extension for communicating with Apache Solr server
php-ssh2 - Bindings for the libssh2 library
php-stomp - Streaming Text Oriented Messaging Protocol (STOMP) client module for PHP
php-tideways - Tideways PHP Profiler Extension
php-uploadprogress - file upload progress tracking extension for PHP
php-uuid - PHP UUID extension
php-yac - YAC (Yet Another Cache) for PHP
php-yaml - YAML-1.1 parser and emitter for PHP
php-zmq - ZeroMQ messaging bindings for PHP
php7.0-bcmath - Bcmath module for PHP
php7.0-bz2 - bzip2 module for PHP
php7.0-dba - DBA module for PHP
php7.0-enchant - Enchant module for PHP
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php7.0-imap - IMAP module for PHP
php7.0-interbase - Interbase module for PHP
php7.0-intl - Internationalisation module for PHP
php7.0-mbstring - MBSTRING module for PHP
php7.0-mcrypt - libmcrypt module for PHP
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-soap - SOAP module for PHP
php7.0-sybase - Sybase module for PHP
php7.0-xsl - XSL module for PHP (dummy)
php7.0-zip - Zip module for PHP

I'm working with ubuntu and my version is:

enter image description here

What can I do to use utf8_encode or utf8_decode functions?

Update 1:

I have tried to install php7.0-common and I have got this result:

php7.0-common is already in its most recent version 
(7.0.18-1+deb.sury.org~yakkety+1).

How can I install php7.0-xml module?

Update 2:

I have to try to install sudo apt-get install php7.0-mbstring but I have yet installed this module.

And I have to try to install this other module sudo apt-get install php7.0-xmlrpc and I have got the same result:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 php7.0-xml : Depends: php7.0-common (= 7.0.18-0ubuntu0.17.04.1) 
but 7.0.18-1+deb.sury.org~yakkety+1 is to be installed
E: Unable to correct problems, you have held broken packages.

It doesn't work for me.

Update 3: Original set up: Web codified in UTF-8 and MySQL database with default character set latin and collate latin1_spanish_ci;.

From the web there is a moment where we ask for an address to the server and in the address we've got special characters:

enter image description here

We are going to return the row with id = 2. In field "direccion" we've got this character "ü". The server response to the request without encoding the address and I've got:

The request is OK.

enter image description here

But the response:

enter image description here

There is nothing, no response!

Right now, we are going to codified the address like @duskwuff has recommend:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $row["address"])

The request is the same but we've got a response now!!!

enter image description here

If we print the address in the web page, we've got:

enter image description here

Update 4:

Next test ... We are going to do the same test than an update 3 but changing MySQL database default character and collation. We change our database to default character UTF8 and collation utf8_spanish_ci. Our web page is still codified with UTF8, and the response from the server is without encoding address.

If I make a select to database we can see all the data correctly.

enter image description here

Ok. I'm going to make a request and response but without coding anything. Don't forget, the data from the database are saved in UTF8.

The request is OK, but the request:

enter image description here

There is no response, like on the update 3.

But, if we codify the response with the recommendation of @duskwuff:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $row["direccion"]);

enter image description here

We've got a response; and we can see the data printed on the we page:

enter image description here

Therefore, I don't know why but I have to codify the data to UTF8 to get a response.

I create the schema and table with:

CREATE SCHEMA IF NOT EXISTS `31juegos` DEFAULT CHARACTER SET utf8 COLLATE utf8_spanish_ci;

CREATE TABLE IF NOT EXISTS `31juegos`.`tbl006_direccion` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `jugador_id` INT UNSIGNED NOT NULL,
  `provincia_id` INT UNSIGNED NOT NULL,
  `direccion` VARCHAR(100) NOT NULL,
  `ciudad` VARCHAR(50) NOT NULL,
  `codigo_postal` VARCHAR(5) NOT NULL,
  `pais` VARCHAR(50) NOT NULL,
  `telefono` VARCHAR(12) NULL,
  `fecha` DATETIME NOT NULL DEFAULT now(),
  PRIMARY KEY (`id`),
  INDEX `fk_tbl006_direccion_tbl005_jugador1_idx` (`jugador_id` ASC),
  INDEX `fk_tbl006_direccion_tbl020_provincia1_idx` (`provincia_id` ASC),
  CONSTRAINT `fk_tbl006_direccion_tbl005_jugador1`
    FOREIGN KEY (`jugador_id`)
    REFERENCES `31juegos`.`tbl005_jugador` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_tbl006_direccion_tbl020_provincia1`
    FOREIGN KEY (`provincia_id`)
    REFERENCES `31juegos`.`tbl020_provincia` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

The field that we codify is:

`direccion` VARCHAR(100) NOT NULL,
halfer
  • 18,701
  • 13
  • 79
  • 158
José Carlos
  • 2,170
  • 7
  • 43
  • 78
  • 1
    [utf8_(en|de)code removed from php7?](http://stackoverflow.com/q/35701730/1415724) and [Function utf8_encode undefined](http://stackoverflow.com/questions/38337444/function-utf8-encode-undefined/38341255). Did you see those? – Funk Forty Niner May 11 '17 at 21:31
  • From the url's that you've gave me I have applied two possible solutions sudo a2enmod xml2enc and sudo apt-get php-patchwork-urf8 and it doesn't work to me :( – José Carlos May 11 '17 at 21:44
  • Why do you need it? – zerkms May 11 '17 at 23:05
  • I need to return some data from mysql. These data has special characters liks "ñ", "áéíóú", "ç", etc. – José Carlos May 11 '17 at 23:07
  • @JoséCarlos you don't need `utf8_encode` to fetch utf-8 data from database for sure. – zerkms May 11 '17 at 23:23
  • Can I use any other function? – José Carlos May 11 '17 at 23:24
  • You don't need any function at all. If everything is setup correctly - you don't need to do anything special. I believe you're currently solving the consequences of the wrong code and/or incorrect configuration, instead of just doing it properly. – zerkms May 11 '17 at 23:25
  • I have defined the coding of my web in utf-8 and mysql coding is latin1 COLLATE latin1_spanish_ci; – José Carlos May 11 '17 at 23:27
  • `latin1` is totally wrong for unicode. – zerkms May 11 '17 at 23:27
  • If I want to represent characters like "ñ", "ç", "ü", which is the best encoding? – José Carlos May 11 '17 at 23:29
  • How about `utf8`? – zerkms May 11 '17 at 23:29
  • utf8 cannot represent this characters "ñ", "ç", "ü". I want to save in my database words like "camión", "cigüeña", "España", etc. – José Carlos May 11 '17 at 23:31
  • "utf8 cannot represent this characters" --- that's not true. http://www.fileformat.info/info/unicode/char/f1/index.htm Unicode can represent every character from every other character set and even much-much more – zerkms May 11 '17 at 23:32
  • If I'm not wrong this page: http://www.fileformat.info/info/unicode/char/f1/index.htm is talking about HTML encoding. I'm talking about MySQL encoding, because I want to save words in my Database like "camión", "riñón", "España", "cigüeña", "Barça", etc. – José Carlos May 11 '17 at 23:36
  • Anyway, with php5.x there is no problem to use and install utf8_encode() and utf8_decode() functions, and I would like to still using these functions. So, I need to know how to install it. Thanks. – José Carlos May 11 '17 at 23:38
  • "If I'm not wrong" --- you're wrong. If you check it all - it shows different encodings, including utf-8. https://unicode-table.com/en/00F1/ "there is no problem to use and install utf8_encode" --- it makes no sense. I seriously cannot understand why you prefer to re-encode data multiple times instead of just doing it in the right way. But it's up to you, have fun. – zerkms May 11 '17 at 23:43
  • Hi!!! You are right!!! I have change the default character and collation to UTF-8 and I can save words with "ñ", "ç", "ü", for example. But, still doesn't wrok to me. Because when I try to return to the web a word with one of that characters it doesn't work. I make a request to the server using angularjs and when I try to get the response I don't get anything. If I remove the data which contains that characters then works fine. So I need to codified these data. In other development I codified these data and get the correctly response. – José Carlos May 11 '17 at 23:59
  • "It does not work" --- is not a technical explanation for the problem. Make a research and provide some details you need to help with. Or better - check the provided answer and ask another question if you have problems dealing with unicode data. – zerkms May 12 '17 at 00:10
  • When I say "It does not work" I mean that from server send the response but these response doesn't arrive to the page. If I send data without encoding I don't have any response for the request. – José Carlos May 12 '17 at 00:12
  • "doesn't arrive to the page" --- that sounds unbelievable, that's why I asked details. If you send something - it certainly would be delivered. So, checkmark the provided answer and ask a separate question. There you specify as detailed as possible what you do, how you do it and what you get (possibly with screenshots of the browser network interactions) – zerkms May 12 '17 at 00:13
  • I'm going to update my original post with more information. – José Carlos May 12 '17 at 00:15
  • It must be `utf8` encoding for the columns that hold your data, not `latin`. You must convert your database to utf-8. – zerkms May 12 '17 at 00:51
  • I'm going to publish more information in my original post. – José Carlos May 12 '17 at 00:52
  • Btw, you don't get response back because JSON expects data to be UTF-8, while yours is not. – zerkms May 12 '17 at 00:53
  • Correct!!! So, I need to codified!!! I'm preparing a new update. Please, wait it. Thanks in advance!!! – José Carlos May 12 '17 at 00:55
  • So, what's your problem now? With that table you don't need any additional encoding conversion. – zerkms May 12 '17 at 02:12

1 Answers1

5

Do not use the utf8_encode() or utf8_decode() functions. They have misleading names -- they actually perform conversions between ISO-8859-1 and UTF-8. In most situations, they should not be necessary; your database and application should use UTF-8 text throughout, and should never deal with ISO-8859-1 text at all.

If you actually need to convert text from ISO-8859-1 to UTF-8 -- which is what utf8_encode() does -- use the iconv() function:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $iso_8859_1_text);

Avoid the utf8_decode() function entirely. It converts UTF-8 text to ISO-8859-1, and will replace any character which isn't available in ISO-8859-1 with a question mark. This will mangle text written in many European languages, and will make non-Latin text (like Russian or Chinese) completely unreadable.

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269
  • The thing is that they don't even have `ISO-8859-1`. Their initial data is utf-8, which they then store in latin1 and then need to convert it back to utf8 on retrieval. – zerkms May 11 '17 at 23:44
  • @zerkms Hence my note that these functions shouldn't be necessary at all. – duskwuff -inactive- May 11 '17 at 23:49
  • I had to use `utf8_decode`/`utf8_encode` when reading/writing file names on windows. However, it appears that this is not necessary on Linux. – Aloso Dec 18 '17 at 20:59