/** * Removes strings, respecting escaped quotes. * * @access private * @return string */ public static function all_substring_strings($string) { $str = str_replace('\\"', '[[ESCAPED_STRING]]', $string); $strings = stack_utils::all_substring_between($str, '"'); foreach ($strings as $key => $string) { $strings[$key] = str_replace('[[ESCAPED_STRING]]', '\\"', $string); } return $strings; }
public function test_all_substring_between() { $this->assertEquals(array(), stack_utils::all_substring_between('hello world!', '[', ']')); $this->assertEquals(array('hello'), stack_utils::all_substring_between('[hello] world!', '[', ']')); $this->assertEquals(array('hello', 'world'), stack_utils::all_substring_between('[hello] [world]!', '[', ']')); $this->assertEquals(array(), stack_utils::all_substring_between('hello world!', '$')); $this->assertEquals(array('hello'), stack_utils::all_substring_between('$hello$ world!', '$')); $this->assertEquals(array('hello', 'world'), stack_utils::all_substring_between('$hello$ $world$!', '$')); // This is current behaviour, but I am not sure it is correct. $this->assertEquals(array('hello', 'wor'), stack_utils::all_substring_between('[he[llo] [wor]ld]!', '[', ']')); }
/** * Extract the CAS commands from the string * * @access public * @return bool false if no commands to extract, true if succeeds. */ private function extract_cas_commands() { // First check contains @s. $count = preg_match_all('~(?<!@)@(?!@)~', $this->trimmedcastext, $notused); if ($count == 0) { // Nothing to do. return null; } else { // Extract the CAS commands. $temp = stack_utils::all_substring_between($this->trimmedcastext, '@', '@', true); // Create array of commands matching with their labels. $i = 0; $valid = true; $errors = ''; $cmdarray = array(); $labels = array(); $sessionkeys = array(); if (is_a($this->session, 'stack_cas_session')) { $sessionkeys = $this->session->get_all_keys(); } foreach ($temp as $cmd) { // Trim of surrounding white space and CAS commands. $cmd = stack_utils::trim_commands($cmd); $cs = new stack_cas_casstring($cmd); $cs->get_valid($this->security, $this->syntax, $this->insertstars); do { // ... make sure names are not already in use. $key = 'caschat' . $i; $i++; } while (in_array($key, $sessionkeys)); $sesionkeys[] = $key; $labels[] = $key; $cs->set_key($key, true); $cmdarray[] = $cs; $valid = $valid && $cs->get_valid(); $errors .= $cs->get_errors(); } if (!$valid) { $this->valid = false; $this->errors .= stack_string('stackCas_invalidCommand') . '</br>' . $errors; } if (!empty($cmdarray)) { $newsession = $this->session; if (null === $newsession) { $newsession = new stack_cas_session($cmdarray, null, $this->seed); } else { $newsession->add_vars($cmdarray); } $this->session = $newsession; // Now replace the commannds with their labels in the text. $this->trimmedcastext = stack_utils::replace_between($this->trimmedcastext, '@', '@', $labels, true); } } }