PHP7.0:调用未定义的函数 utf8_encode()

Jos*_*los 1 php php-7 ubuntu-16.04

我正在使用 PHP 7.0 进行开发,并且尝试使用函数 utf8_encode() 并且出现错误“调用未定义的函数 utf8_encode()”

我必须尝试使用​​ sudo apt-get install php7.0-xml 安装 php7.0-xml 模块,但出现此错误:

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.
Run Code Online (Sandbox Code Playgroud)

如果我让sudo apt-cache search php7.0-*我得到这个结果:

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
Run Code Online (Sandbox Code Playgroud)

我正在使用 ubuntu,我的版本是:

在此处输入图片说明

如何使用 utf8_encode 或 utf8_decode 函数?

更新 1:

我尝试安装 php7.0-common 并得到以下结果:

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

如何安装 php7.0-xml 模块?

更新 2:

我必须尝试安装sudo apt-get install php7.0-mbstring但我还没有安装这个模块。

我必须尝试安装这个其他模块sudo apt-get install php7.0-xmlrpc并且我得到了相同的结果:

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.
Run Code Online (Sandbox Code Playgroud)

它对我不起作用。

更新 3: 原始设置:Web 编码在 UTF-8 和 MySQL 数据库中,带有default character set latincollate latin1_spanish_ci;

在网络上,有一段时间我们要求服务器的地址,并且在地址中我们有特殊字符:

在此处输入图片说明

我们将返回 id = 2 的行。在“direction”字段中,我们有这个字符“ü”。服务器对请求的响应没有对地址进行编码,我得到了:

请求没问题。

在此处输入图片说明

但回应是:

在此处输入图片说明

什么都没有,没有反应!

现在,我们将像@duskwuff 推荐的那样对地址进行编码:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $row["address"])
Run Code Online (Sandbox Code Playgroud)

请求是一样的,但我们现在得到了回应!!!

在此处输入图片说明

如果我们在网页中打印地址,我们将得到:

在此处输入图片说明

更新 4:

下一个测试...我们将进行与更新 3 相同的测试,但更改 MySQL 数据库默认字符和排序规则。我们将数据库更改为默认字符 UTF8 和排序规则 utf8_spanish_ci。我们的网页依然是UTF8编码,服务器响应没有编码地址。

如果我选择数据库,我们可以正确地看到所有数据。

在此处输入图片说明

好的。我将发出请求和响应,但不进行任何编码。别忘了,数据库中的数据是以UTF8格式保存的。

请求没问题,但请求:

在此处输入图片说明

没有响应,就像在更新 3 上一样。

但是,如果我们按照@duskwuff 的建议编写响应:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $row["direccion"]);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我们得到了回应;我们可以看到我们页面上打印的数据:

在此处输入图片说明

因此,我不知道为什么,但我必须将数据编码为 UTF8 才能得到响应。

我创建架构和表:

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;
Run Code Online (Sandbox Code Playgroud)

我们编纂的领域是:

`direccion` VARCHAR(100) NOT NULL,
Run Code Online (Sandbox Code Playgroud)

小智 5

不要使用utf8_encode()utf8_decode()函数。它们具有误导性的名称——它们实际上在 ISO-8859-1 和 UTF-8 之间进行转换。在大多数情况下,它们不是必需的;您的数据库和应用程序应该始终使用 UTF-8 文本,并且根本不应该处理 ISO-8859-1 文本。

如果您确实需要将文本从 ISO-8859-1 转换为 UTF-8——这是什么utf8_encode()——使用该iconv()函数:

$utf8_text = iconv("ISO-8859-1", "UTF-8", $iso_8859_1_text);
Run Code Online (Sandbox Code Playgroud)

utf8_decode()完全避免该功能。它将 UTF-8 文本转换为 ISO-8859-1,并将用问号替换 ISO-8859-1 中不可用的任何字符。这将破坏用许多欧洲语言编写的文本,并使非拉丁文本(如俄语或中文)完全不可读。