Home Backend Development PHP Tutorial 洪恩在线成语词典小偷程序php版_php技巧

洪恩在线成语词典小偷程序php版_php技巧

May 17, 2016 am 09:11 AM

主要函数是file_get_contents,主程序分两段,跟我一起看过来吧(凡人博客原创代码,转载请注明)。

复制代码 代码如下:

function escape($str){
preg_match_all('/[\x80-\xff].|[\x01-\x7f]+/',$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v){
if(ord($v[0]) $ar[$k] = rawurlencode($v);
else
$ar[$k] = '%u'.bin2hex(iconv('GB2312','UCS-2',$v));
}
return join('',$ar);
}

上面的函数主要是用php实现JavaScript的escape编码过程,因为洪恩的查询接口需要传递过去的值是经过escape编码后的成语条目。
复制代码 代码如下:

function chacy($chengyu){
$chengyu=escape($chengyu);
$text=@file_get_contents('http://study.hongen.com/dict/ndsearchchengyu.aspx?type=exact&word='.$chengyu);
$pos1=strpos($text,'');
$pos2=strrpos($text,'
');
$text=substr($text,$pos1,$pos2-$pos1);
//把字符集由原来的UTF-8转换到GB2312,注意在GB2312之后加上了//IGNORE,强制遇到特殊字符也继续转换,因为在遇到汉字“一”的时候iconv函数会终止转换
$text=iconv('UTF-8','GB2312//IGNORE',$text);
if (strpos($text,'出处')){
return $text;
}
}

上面是自己定义的成语查询函数,首先escape编码要查询的成语条目,然后使用file_get_contents函数获取“http://study.hongen.com/dict/ndsearchchengyu.aspx?type=exact&word=”页面查询的内容,使用substr去除前后一些不需要的多余代码,中间就是成语条目的解释部分(包括拼音、解释、出处、例句),最后记得要转码,洪恩返回的结果是UTF-8编码,一般情况下我们需要转换为GB2312编码,上面我写了一个有关iconv函数转换编码有时会出现bug的注释文本,需要加上一个//IGNORE参数。最后判断结果中是否存在“出处”二字,有的话说明整个函数运行成功,可以把获取到的内容return给页面了。

程序主体实现完成,只需在相应位置调用查询函数:chacy 就可以了。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

See all articles