public function test_valueForJSInsert() { $expected = "\\'"; $string = "'"; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\"'; $string = '"'; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\/'; $string = '/'; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\x3e'; $string = '>'; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\x3c'; $string = '<'; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\n'; $string = "\n"; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\r'; $string = "\r"; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\n'; $string = " "; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\n'; $string = " "; $this->assertSame($expected, valueForJSInsert($string)); $expected = '\\\\'; $string = '\\'; $this->assertSame($expected, valueForJSInsert($string)); }
/** * Create JavaScript source from array * @param array ar parameter array * @param string prefix strings for the prefix for key * @param array exarray array containing excluding keys * @return string JavaScript source */ function arrayToJSExcluding($ar, $prefix, $exarray) { $returnStr = ''; if (is_array($ar)) { $items = array(); foreach ($ar as $key => $value) { $items[] = arrayToJSExcluding($value, $key, $exarray); } $currentKey = (string) $prefix; foreach ($items as $item) { if (!in_array($currentKey, $exarray) && $item != '') { if ($returnStr == '') { $returnStr .= $item; } else { $returnStr .= ',' . $item; } } } if ($currentKey == '') { $returnStr = '{' . $returnStr . '}'; } else { $returnStr = "'{$currentKey}':{" . $returnStr . '}'; } } else { $currentKey = (string) $prefix; if ($currentKey == '') { $returnStr = "'" . valueForJSInsert($ar) . "'"; } else { if (!in_array($currentKey, $exarray)) { $returnStr = "'{$prefix}':'" . valueForJSInsert($ar) . "'"; } } } return $returnStr; }