Home >Backend Development >PHP Tutorial >Let PHP support yar.packager to achieve text highlighting effect_PHP tutorial
1. Install three PHP extensions: msgpack, yar, and yaf
2. When compiling yar, use ./configure --enable-msgpack --with-php-config=/usr/local/ php/bin/pgp-config parameter, the --enable-msgpack parameter is to enable the packager's support for yar
3. php -i|grep msgpack, if there is yar.packager => msgpack => msgpack Note that yar already supports msgpack
4. Test page
class YarCheckKeyword {
protected static $HOSTNAME = 'kwdt.yarc.service.weibo.com';
protected static $PORT = '7002';
/**
*
* Request Kwdt Server
*
* @param string $text Text string
*
* @param array $types Keyword type
*
* @param int $return_text Whether to return the hit keyword 1. Yes 0. No No need to return here
*
* @return array
*
*/
public function connectKwdt_Server($text, $return_text = 1, $types=array(1, 2, 3), $ withoutsass = false) {
if (!class_exists("Yar_client") || !$text || !$types) {
return "yar_client no existsn";
}
$funcname = " detect";
$host = YarCheckKeyword::$HOSTNAME;
$port = YarCheckKeyword::$PORT;
try{
$client = new Yar_Client("tcp://$host:$ port");
$response = $client->$funcname($text, $return_text, $types);
return $response;
}catch (Exception $e){
print_r ($e);
}
}
}
$text='aaaaaaaaaaa';
$obj=new YarCheckKeyword();
$a=$obj->connectKwdt_Server ($text);
print_r($a);
5. Save the above code into a file and execute it using php. The test result is as follows, indicating success
Array
(
[0] => -1
[1] => no keyword occured
)