/** * Set array of flashcard ids, from a selection expressed as a string. * * Accepts: * Single cards 3 * Range of cards 4-25 * Kanji <single utf8 char> * * Delimiters: * All flashcard ids (numerical or kanji) must be separated by commas, * or spaces, or tabs. A range of cards can not have spaces around the dash. * Kanji characters do not need to be separated between them but must be separated * from the numerical indices eg: * * 3, 42 15, 10-13 一年生 * * @param string $selString Selection in string format * * @return int Number of cards in selection */ public function setFromString($selString) { $this->itemIds = array(); // split string on spaces, japanese space (0x3000) and comma $selparts = preg_split('/[,\\s\\x{3000}]+/u', $selString, -1, PREG_SPLIT_NO_EMPTY); if (!count($selparts)) { return false; } foreach ($selparts as &$part) { // numerical range if (preg_match('/^([0-9]+)-([0-9]+)$/', $part, $matches)) { $from = $matches[1]; $to = $matches[2]; if (!rtkBook::isValidRtkFrameNum($from) || !rtkBook::isValidRtkFrameNum($to)) { $this->request->setError('if', sprintf('Invalid framenumber: "%s"', $part)); return false; } elseif ($from > $to) { $this->request->setError('ir', sprintf('Invalid range: "%s"', $part)); return false; } for ($i = $from; $i <= $to; $i++) { $this->itemIds[] = $i; } } elseif (ctype_digit($part)) { $framenum = intval($part); if (!rtkBook::isValidRtkFrameNum($framenum)) { $this->request->setError('if', sprintf('Invalid framenumber: "%s"', $part)); return false; } $this->itemIds[] = $framenum; } elseif (CJK::hasKanji($part)) { $cjkChars = CJK::getKanji($part); if (!count($cjkChars)) { continue; } foreach ($cjkChars as $cjk) { $framenum = rtkBook::getIndexForKanji($cjk); if ($framenum) { $this->itemIds[] = $framenum; } else { $this->request->setError('if', sprintf('Cannot add non-Heisig character: "%s"', $part)); return false; } } } else { $this->request->setError('ip', sprintf('Invalid part: "%s"', $part)); return false; } } // remove duplicates $this->itemIds = array_unique($this->itemIds); return $this->getNumCards(); }
/** * Search for a kanji by keyword. The search term is an exact keyword, * or part of a keyword. Multiple edition keywords with slashes should * replace the slash with underscore first (cf MySQL LIKE operator). * * @return mixed Frame number, or FALSE if no results. */ public static function getFramenumForSearch($sSearch) { $s = trim($sSearch); //$s = preg_replace('/[^0-9a-zA-Z-\.\' \[\]\(\)]/', '', $s); if (CJK::hasKanji($s)) { // it's not a western character.. /* 0x3000 http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml */ self::getInstance()->select('framenum')->where('kanji = ?', $s)->query(); return ($row = self::$db->fetchObject()) ? $row->framenum : false; } elseif (preg_match('/^[0-9]+$/', $s)) { // check if frame number is valid $framenum = intval($s); return self::getInstance()->count('framenum = ?', $framenum) ? $framenum : false; } elseif (preg_match('/[^0-9]/', $s)) { // search on keyword // acount for multiple edition keyword // try to find an exact match self::getInstance()->select('framenum')->where('keyword = ? OR keyword LIKE ? OR keyword LIKE ?', array($s, $s . '/%', '%/' . $s))->query(); if ($row = self::$db->fetchObject()) { return $row->framenum; } elseif (strlen($s) < 3) { return false; } else { self::getInstance()->select('framenum')->where('keyword LIKE ?', '%' . $s . '%')->query(); return ($row = self::$db->fetchObject()) ? $row->framenum : false; } } return false; }