Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式正在销毁我的UTF-8XML(PHP)_Php_Regex_Xml_Utf 8 - Fatal编程技术网

正则表达式正在销毁我的UTF-8XML(PHP)

正则表达式正在销毁我的UTF-8XML(PHP),php,regex,xml,utf-8,Php,Regex,Xml,Utf 8,我有个问题。。我有一个代码,下载一些XML文件并删除一些我不需要的标记。从那以后,一切都被发现了。我的XML文件是UTF-8格式的,没有问题 但是,由于我添加了一个代码来替换和更改标题值,我的XML文件在UTF-8中不长,因此我收到以下错误消息: "D:\Anwendung\PHP 7\php-win.exe" C:\Users\Jan\PhpstormProjects\censored\test.php PHP Warning: DOMDocument::load(): Input is n

我有个问题。。我有一个代码,下载一些XML文件并删除一些我不需要的标记。从那以后,一切都被发现了。我的XML文件是UTF-8格式的,没有问题

但是,由于我添加了一个代码来替换和更改标题值,我的XML文件在UTF-8中不长,因此我收到以下错误消息:

"D:\Anwendung\PHP 7\php-win.exe" C:\Users\Jan\PhpstormProjects\censored\test.php
PHP Warning:  DOMDocument::load(): Input is not proper UTF-8, indicate encoding !
Bytes: 0xE3 0xA4 0x63 0x68 in file:/C:/Users/Jan/PhpstormProjects/censored/data/gamesplanet.xml, line: 1423 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 18
PHP Fatal error:  Uncaught Error: Call to a member function getElementsByTagName() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:23
Stack trace:
#0 C:\Users\Jan\PhpstormProjects\censored\test.php(86): countAd('data/gamesplane...')
#1 {main}
  thrown in C:\Users\Jan\PhpstormProjects\censored\test.php on line 23

Process finished with exit code 255
第1423行代表:
W㥣赫特尔·冯·米特勒德

如果我不仔细阅读下面的代码,我就不会收到错误消息,这条消息出现在第1423行:
Wächter von mittlerde

有人有主意能帮我吗

代码:


你好,谢谢

您应该使用
u
修饰符激活模式的unicode模式。这意味着您将匹配unicode字符和代码点,而不是单个字节。
Wächter
中的
ä
由多个字节组成,其中一个字节在单字节模式下被解释为字尾

preg_match('(.)u', 'äöü', $match);
var_dump($match);
输出:

array(1) {
  [0]=>
  string(2) "ä"
}
string(4) "_ö_"
如您所见,该示例匹配第一个字符,而不仅仅是第一个字节。 接下来是使用数组作为
preg\u replace()
的参数的可能性。这允许您简化调用

var_dump(preg_replace(['(ä)u', '(ü)u'], '_', 'äöü'));
输出:

array(1) {
  [0]=>
  string(2) "ä"
}
string(4) "_ö_"
但更好的选择可能是在模式中使用字符类和
|
操作符
$replaceNothing
$replaceSpace
是字符数组,可以将它们更改为字符类:

$replaceWithNothing = '([,;`#\'´!().@’+™]+|(?:\b(?:Steam|Eu|Key)\b))u';
$replaceWithSpace = '([-–_/:]+)u';

var_dump(
  preg_replace(
    [$replaceWithNothing, $replaceWithSpace], 
    ['', ' '], 
    'remove (™) and :replace:'
  )
);
对于“替换”一词:

$replaceWords = [
  '(\bAsia\b)ui' => 'ASIA';
  '(\buk\b)ui', 'UK'
);
$output = preg_replace(array_keys($words), $words, $input);
我不知道为什么不为
modifyword()
函数使用简单的替换。您正在将第一次出现的
反击进攻“
替换为
反击全球进攻”


评论中提到使用mb_*函数。我建议使用更现代的。这是PHP中unicode处理的标准、更现代、更强大的扩展。

问题是,您使用的函数不支持多字节字符(
str\u replace
ucwords
strtolower
preg\u replace
,不带u修饰符)和多字节字符串(UTF8)。改为使用
mb\u
函数,并将u修饰符与
preg\u replace
一起使用。请注意
preg\u replace
可以将数组作为第一个和第二个参数。您能给我一个代码片段吗?我如何做到这一点因为我不知道mb_函数是什么意思,你用“u修饰符”是什么意思?请参见和。1)将
strtolower
替换为
mb_strtolower
ucwords
替换为
mb_-ucwords
,等等。2)在regexps(“/something/iu”)的末尾添加
u
。但这一项并不能解决我的
$string=mbstrtolower问题($string,'UTF-8');
问题…某种程度上,它比
strtolower()
破坏UTF-8要好,它是一个ANSI(单字节)函数。
ucwords()
。但是我认为不需要转换字符串变量-模式使用
I
(不区分大小写)修饰符。再加上
u
这应该足够了。我只使用strtolower()函数,因为我希望所有字符串都相等。所以我调用strtolower()函数,然后我想说ucwords(),这样它们在mb_*()函数用于替换使用多字节字符串的标准字符串函数。它们是默认安装的一部分,我不希望它们在不久的将来被删除。因此您可以使用它们。不过,我建议您记住ICU函数并了解它们。它们不仅仅是Unicode string功能,但国际化和本地化功能。好的,谢谢!-但我的问题是这一个,我没有得到解决…这很烦人![链接]