Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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
unicode字符与PHP中的wikipedia搜索_Php_Unicode_Wikipedia - Fatal编程技术网

unicode字符与PHP中的wikipedia搜索

unicode字符与PHP中的wikipedia搜索,php,unicode,wikipedia,Php,Unicode,Wikipedia,我将一个PHP字符串传递到wikipedia搜索页面,以便检索部分定义。 除了出现在\u..表单中的unicode字符外,其他所有字符都可以正常工作。下面是一个更好地解释我自己的例子。如您所见,名称的拼音不可读: 亨里克易卜生,亨里克易卜生\u02c8h\u025bn\u027eik\u02c8ips\u0259n (斯基恩,1828年马尔佐20日-奥斯陆,1906年马焦23日)埃斯塔托·乌诺·斯克里托, 德拉姆塔戈,挪威人 我从维基百科获取代码片段的代码如下: $word = $_GET["w

我将一个PHP字符串传递到wikipedia搜索页面,以便检索部分定义。 除了出现在\u..表单中的unicode字符外,其他所有字符都可以正常工作。下面是一个更好地解释我自己的例子。如您所见,名称的拼音不可读:

亨里克易卜生,亨里克易卜生\u02c8h\u025bn\u027eik\u02c8ips\u0259n (斯基恩,1828年马尔佐20日-奥斯陆,1906年马焦23日)埃斯塔托·乌诺·斯克里托, 德拉姆塔戈,挪威人

我从维基百科获取代码片段的代码如下:

$word = $_GET["word"];
$html = file_get_contents('https://it.wikipedia.org/w/api.php?action=opensearch&search='.$word);
$utf8html = html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", $html), ENT_NOQUOTES, 'UTF-8');
我的代码的最后一行并不能解决这个问题。
您知道如何获得完全可读的干净文本吗?

如果正则表达式字符串中有一些错误,请尝试使用:

<?php
 $str = "Henrik Ibsen, Henrik Ibsen \u02c8h\u025bn\u027eik \u02c8ips\u0259n(Skien, 20 marzo 1828 - Oslo, 23 maggio 1906) è stato uno scrittore, drammaturgo, poeta e regista teatrale norvegese.";
 $utf8html = preg_replace('@\\\U([0-9A-F]{4})@i', "&#x\\1", $str);
 echo $utf8html;

维基百科搜索API的输出是JSON。不要试图从中挖出一些东西来解析字符串文字,这样你就不会发疯了。只需使用现成的JSON解析器

此外,当您将单词添加到查询字符串中时,需要对其进行URL转义,否则任何对中带有URL特殊字符的单词的搜索都将失败

总之:

$word = $_GET['word'];
$url = 'https://it.wikipedia.org/w/api.php?action=opensearch&search='.urlencode($word);
$response = json_decode(file_get_contents($url));

$matching_titles_array = $response[1];
$matching_summaries_array = $response[2];
$matching_urls = $response[3];
...etc...

好吧,bobince发布的答案肯定比我之前的程序更有效,我之前的程序旨在一点一点地删除和修剪我需要的东西。为了向您展示我是如何做的,以下是我以前的代码:

$html = file_get_contents('https://it.wikipedia.org/w/api.php?action=opensearch&search='.$s);
$decoded = preg_replace('@\\\U([0-9A-F]{4})@i', "&#x\\1", $html);
$par = array("[", "]");
$def_no_par = str_replace($par, "", $decoded);
$def_no_vir = str_replace("\"\",", "", $def_no_par);
$def_cap = str_replace("\",", "\",<br>", $def_no_vir);
$def_pulita = str_replace("\"", "", $def_cap);
$def_clean = str_replace(".,", ".", $def_pulita);
$definizione = str_replace("$s,", "", $def_clean);
$out = str_replace("\\", "\"", $definizione);
$html=file\u get\u contents('https://it.wikipedia.org/w/api.php?action=opensearch&search=“.$s);
$decoded=preg_replace('@\\\U([0-9A-F]{4})@i',“&#x\\1',$html);
$par=数组(“[”,“]”);
$def_no_par=str_replace($par,“,$decoded);
$def\u no\u vir=str\u replace(“\”,“,”,$def\u no\u par);
$def\u cap=str\u replace(“\”、“,“\”、
”、$def\u no\u vir); $def_pulita=str_replace(“\”,“,$def_cap”); $def_clean=str_replace(“,”,“,$def_pulita”); $definizione=str_replace($s,“”,$def_clean); $out=str\u替换(“\\”、“\\”、$definizione);
正如您所看到的,删除部分输出以使其更具可读性是相当令人厌烦的(而且并不完全成功)。 使用JSON方法可以使一切更加线性化。以下是我的新解决方法:

$search = 'https://it.wikipedia.org/w/api.php?action=opensearch&search='.urlencode($s);
$response = json_decode(file_get_contents($search));
$matching_titles_array = $response[1];
$matching_summaries_array = $response[2];
$matching_urls = $response[3];

echo '<h3><div align="center"><font color=" #A3A375">'.$titolo.'</font></div></h3><br><br>';
foreach($response[1] as $t) {
    echo '<font color="#5C85D6"><b>'.$t.'</b></font><br><br>';
}
foreach($response[2] as $s) {
    echo $s.'<br><br>';
}
foreach($response[3] as $l) {
$link = preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="$1" target="_blank">$1</a>', $l);
    echo $link.'<br><br>';
}
$search=https://it.wikipedia.org/w/api.php?action=opensearch&search='.urlencode($s);
$response=json_解码(文件获取内容($search));
$matching_titles_array=$response[1];
$matching_summaries_array=$response[2];
$matching_url=$response[3];
回音“.$titolo.”

; foreach($response[1]为$t){ 回显“.$t.”

”; } foreach($s作为响应[2]{ 回显$s.

'; } foreach($response[3]为$l){ $link=preg_replace(“!”((f|ht)tp(s):/)[-a-zA-Zа-a-a-a-a-a-u()()0-9:%:%。~#?&;/=]+)!i',“$l”); echo$link.“

”; }
优点是现在我可以随心所欲地操纵数组了。
您可以在操作中看到它:

只需使用
json_decode
$utf8html=json_decode(“'.$html.”)工作。此解决方案对我不起作用,可能是因为我的
$html
变量不是简单的文本字符串-它是
文件\u get\u contents()
的输出。这里有一些问题,您无法得到您期望的结果(a)其他反斜杠转义,如
\”
\n
;(b)U+00FFFF以上的字符;(c)任何
\U
序列,后跟
[0-9A-Fa-f]
范围内的文字字符(由于缺少字符引用分号终止符)。