/** * 把用户输入的字符串转换为 UTF-8 编码 * @param string $buf 要转换字符串 * @return string 转换后的字符串 */ public static function convertIn($buf) { if (self::$charset !== null) { return XS::convert($buf, 'UTF-8', self::$charset); } return $buf; }
public function testSetCharset() { $text = '全文检索'; $tk = $this->object; $words = $tk->getResult($text); $this->assertEquals($words[0]['word'], $text); $gbk = XS::convert($text, 'GBK', 'UTF-8'); $tk->setCharset('GBK'); $words = $tk->getResult($gbk); $this->assertEquals($words[0]['word'], $gbk); }
$input = dirname(__FILE__) . '/skel'; $dir = dir($input); while (($entry = $dir->read()) !== false) { if ($entry === '.' || $entry === '..') { continue; } if (substr($entry, -3) === '.in') { echo "正在生成 " . substr($entry, 0, -3) . " ...\n"; $file = $output . '/' . substr($entry, 0, -3); if (file_exists($file)) { copy($file, $file . '.bak'); } $content = file_get_contents($input . '/' . $entry); $content = strtr($content, $vars); if ($vars['@charset@'] !== 'UTF-8') { $content = XS::convert($content, $vars['@charset@'], 'UTF-8'); } file_put_contents($file, $content); } else { echo "正在复制 " . $entry . " ...\n"; $file = $output . '/' . $entry; if (is_dir($input . '/' . $entry)) { XSUtil::copyDir($input . '/' . $entry, $file); } else { if (file_exists($file)) { copy($file, $file . '.bak'); } copy($input . '/' . $entry, $file); } } }
function xs_output_encoding($buf) { return XS::convert($buf, $GLOBALS['oe'], 'UTF-8'); }
public static function setUpBeforeClass() { self::$data = array('pid' => 1234, 'subject' => "Hello, 测试标题", 'message' => "您好,这儿是真正的测试内容\n另起一行用英文\n\nHello, the world!", 'chrono' => time()); self::$data_gbk = XS::convert(self::$data, 'GBK', 'UTF-8'); }
public function testConvert() { $str = 'english string only'; $this->assertEquals(XS::convert($str, 'GBK', 'UTF-8'), $str); $str = 'english 和中文混合'; $str1 = XS::convert($str, 'GBK', 'UTF-8'); $this->assertEquals(XS::convert($str1, 'UTF-8', 'GBK'), $str); }
/** * 初始始化高亮替换数据 */ private function initHighlight() { $terms = array(); $tmps = $this->terms($this->_highlight, false); for ($i = 0; $i < count($tmps); $i++) { if (strlen($tmps[$i]) !== 6 || ord(substr($tmps[$i], 0, 1)) < 0xc0) { $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8'); continue; } // auto fixed duality in libscws // ABC => AB,BC => ABC,BC,AB // ABCD => AB,BC,CD => CD,ABC,BC,AB // ABCDE => AB,BC,CD,DE => CDE,DE,CD,ABC,BC,AB for ($j = $i + 1; $j < count($tmps); $j++) { if (strlen($tmps[$j]) !== 6 || substr($tmps[$j], 0, 3) !== substr($tmps[$j - 1], 3, 3)) { break; } } if (($k = $j - $i) === 1) { $terms[] = XS::convert($tmps[$i], $this->_charset, 'UTF-8'); } else { $i = $j - 1; while ($k--) { $j--; if ($k & 1) { $terms[] = XS::convert(substr($tmps[$j - 1], 0, 3) . $tmps[$j], $this->_charset, 'UTF-8'); } $terms[] = XS::convert($tmps[$j], $this->_charset, 'UTF-8'); } } } $pattern = $replace = $pairs = array(); foreach ($terms as $term) { if (!preg_match('/[a-zA-Z]/', $term)) { $pairs[$term] = '<em>' . $term . '</em>'; } else { $pattern[] = '/' . strtr($term, array('+' => '\\+', '/' => '\\/')) . '/i'; $replace[] = '<em>$0</em>'; } } $this->_highlight = array(); if (count($pairs) > 0) { $this->_highlight['pairs'] = $pairs; } if (count($pattern) > 0) { $this->_highlight['pattern'] = $pattern; $this->_highlight['replace'] = $replace; } }
public function del($term, $field = null) { $field = $field === null ? $this->xs->getFieldId() : $this->xs->getField($field); $cmds = array(); $terms = is_array($term) ? array_unique($term) : array($term); $terms = XS::convert($terms, 'UTF-8', $this->xs->getDefaultCharset()); foreach ($terms as $term) { $cmds[] = new XSCommand(XS_CMD_INDEX_REMOVE, 0, $field->vno, strtolower($term)); } if ($this->_bufSize > 0) { $this->appendBuffer(implode('', $cmds)); } elseif (count($cmds) == 1) { $this->execCommand($cmds[0], XS_CMD_OK_RQST_FINISHED); } else { $cmd = array('cmd' => XS_CMD_INDEX_EXDATA, 'buf' => implode('', $cmds)); $this->execCommand($cmd, XS_CMD_OK_RQST_FINISHED); } return $this; }
private function applySetting($text) { self::$_server->reopen(); foreach ($this->_setting as $key => $cmd) { self::$_server->execCommand($cmd); } return XS::convert($text, 'UTF-8', $this->_charset); }
/** * 智能字符集编码转换 * 将 XS 内部用的 UTF-8 与指定的文档编码按需相互转换 * 索引文档: ... -> UTF-8, 搜索结果文档: ... <-- UTF-8 * @param string $value 要转换的字符串 * @return string 转好的字符串 * @see setCharset */ private function autoConvert($value) { // Is the value need to convert if ($this->_charset === null || $this->_charset == 'UTF-8' || !is_string($value) || !preg_match('/[\\x81-\\xfe]/', $value)) { return $value; } // _meta === null ? index document : search result document $from = $this->_meta === null ? $this->_charset : 'UTF-8'; $to = $this->_meta === null ? 'UTF-8' : $this->_charset; return XS::convert($value, $to, $from); }
public function testGetAddIndex() { $this->assertNull($this->doc2->getAddIndex('subject2')); $this->assertNull($this->doc2->getAddIndex('subject')); $subject = new XSFieldMeta('subject'); $this->doc2->addIndex('subject', 'hello the world'); $this->doc2->addIndex($subject, XS::convert('您好世界', 'GBK', 'UTF-8')); $this->assertEquals(self::$data['subject'], $this->doc2->subject); $this->assertEquals("hello the world\n您好世界", $this->doc2->getAddIndex($subject)); }
private function applySetting($text) { self::$_server->reopen(); foreach ($this->_setting as $key => $cmd) { if (is_array($cmd)) { foreach ($cmd as $_cmd) { self::$_server->execCommand($_cmd); } } else { self::$_server->execCommand($cmd); } } return XS::convert($text, 'UTF-8', self::$_charset); }
private function autoConvert($value) { if ($this->_charset === null || $this->_charset == 'UTF-8' || !is_string($value) || !preg_match('/[\\x81-\\xfe]/', $value)) { return $value; } $from = $this->_meta === null ? $this->_charset : 'UTF-8'; $to = $this->_meta === null ? 'UTF-8' : $this->_charset; return XS::convert($value, $to, $from); }
public function testCharset() { $xs = self::$xs; $search = $xs->search; $query = 'subject:测试'; $docs = $search->search($query); $this->assertEquals(3, count($docs)); $this->assertEquals('测试第二篇', $docs[0]->subject); $this->assertEquals('GBK', $xs->getDefaultCharset()); $search->setCharset($xs->getDefaultCharset()); $this->assertEquals(0, $search->count($query)); $docs = $search->search(XS::convert($query, 'GBK', 'UTF-8')); $this->assertEquals(3, count($docs)); $this->assertEquals(XS::convert('测试第二篇', 'GBK', 'UTF-8'), $docs[0]->subject); }