如何在PHP中以用户友好的方式解析dict输出?
我正在尝试实现字典类型的服务。 我使用cURL和php通过dict协议向dict.org发送请求。 这是我的代码(它自己的作品,可能对未来的读者有帮助): 服务器按预期返回定义以及几个头(我不需要这些头)。响应如下所示:如何在PHP中以用户友好的方式解析dict输出?,php,parsing,curl,dict-protocol,Php,Parsing,Curl,Dict Protocol,我正在尝试实现字典类型的服务。 我使用cURL和php通过dict协议向dict.org发送请求。 这是我的代码(它自己的作品,可能对未来的读者有帮助): 服务器按预期返回定义以及几个头(我不需要这些头)。响应如下所示: 220 miranda.org dictd 1.9.15/rf on Linux 2.6.26-2-686 <auth.mime> <29631663.31530.1250750274@miranda.org> 250 ok 150 3 definiti
220 miranda.org dictd 1.9.15/rf on Linux 2.6.26-2-686 <auth.mime> <29631663.31530.1250750274@miranda.org>
250 ok
150 3 definitions retrieved
151 "Hello" gcide "The Collaborative International Dictionary of English v.0.48"
Hello \Hel*lo"\, interj. & n.
An exclamation used as a greeting, to call attention, as an
exclamation of surprise, or to encourage one. This variant of
{Halloo} and {Holloo} has become the dominant form. In the
United States, it is the most common greeting used in
answering a telephone.
[1913 Webster +PJC]
(... some content removed)
.
250 ok [d/m/c = 3/0/162; 0.000r 0.000u 0.000s]
221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]
220 miranda.org dictd 1.9.15/rf在Linux 2.6.26-2-686上,这是我发送cURL请求的地方)
谢谢大家! 在开始之前,让我声明我不知道dict协议的具体内容
我怀疑你能否创建一个只传递文本的请求。您希望丢弃的信息看起来像状态信息,因此非常有用
我的处理方法如下:
将curl响应数据读入数组,使每一行都是数组中的一个单独条目。可以使用explode()
并在新行字符(\n)处拆分来执行此操作
迭代数组,例如($response as$responseLine){}
您可能需要检查dict协议使用的字符集。我没有提到任何错误处理,但这应该是直截了当的。在我开始之前,让我声明我不知道dict协议的具体内容 我怀疑你能否创建一个只传递文本的请求。您希望丢弃的信息看起来像状态信息,因此非常有用 我的处理方法如下:
explode()
并在新行字符(\n)处拆分来执行此操作您可能需要检查dict协议使用的字符集。我没有提到任何错误处理,但这应该是直截了当的。我就是这样做的。谢谢我就是这样做的。谢谢
220 miranda.org dictd 1.9.15/rf on Linux 2.6.26-2-686 <auth.mime> <29631663.31530.1250750274@miranda.org>
250 ok
150 3 definitions retrieved
151 "Hello" gcide "The Collaborative International Dictionary of English v.0.48"
Hello \Hel*lo"\, interj. & n.
An exclamation used as a greeting, to call attention, as an
exclamation of surprise, or to encourage one. This variant of
{Halloo} and {Holloo} has become the dominant form. In the
United States, it is the most common greeting used in
answering a telephone.
[1913 Webster +PJC]
(... some content removed)
.
250 ok [d/m/c = 3/0/162; 0.000r 0.000u 0.000s]
221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]