/** * 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(); }
public function executeIndex($request) { // handle ajax requests (POST) if ($request->getMethod() !== coreRequest::POST) { $request->setParameter('kanji', 'むかし、むかし、ご存知のとおり、うさぎとかめは、山の上まで競争しました。誰もが、うさぎの方がかめよりも早くそこに着くと思いました。しかし迂闊にも、うさぎは途中で寝てしまいました。目が覚めた時は、もうあとのまつりでした。かめはすでに山のてっ辺に立っていました。'); } else { // filter out all non kanji $s = $request->getParameter('kanji'); $cjk = CJK::getKanji($s); if (!count($cjk)) { $request->setError('foo', 'error'); return; } if (!count($cjk)) { continue; } foreach ($cjk as $cjk) { } } }