Zephir\Utils::addSlashes PHP Method

addSlashes() public static method

Prepares a string to be used as a C-string
public static addSlashes ( string $str, boolean $escapeSlash = false ) : string
$str string
$escapeSlash boolean
return string
    public static function addSlashes($str, $escapeSlash = false)
    {
        $newstr = "";
        $after = null;
        $before = null;
        $length = strlen($str);
        for ($i = 0; $i < $length; $i++) {
            $ch = substr($str, $i, 1);
            if ($i != $length - 1) {
                $after = substr($str, $i + 1, 1);
            } else {
                $after = null;
            }
            switch ($ch) {
                case '"':
                    $newstr .= "\\" . '"';
                    break;
                case "\n":
                    $newstr .= "\\" . 'n';
                    break;
                case "\t":
                    $newstr .= "\\" . 't';
                    break;
                case "\r":
                    $newstr .= "\\" . 'r';
                    break;
                case "\v":
                    $newstr .= "\\" . 'v';
                    break;
                case '\\':
                    switch ($after) {
                        case "n":
                        case "v":
                        case "t":
                        case "r":
                        case '"':
                        case "\\":
                            $newstr .= $ch . $after;
                            $i++;
                            $before = null;
                            continue;
                        default:
                            $newstr .= "\\\\";
                            break;
                    }
                    break;
                default:
                    $newstr .= $ch;
            }
            $before = $ch;
        }
        return $newstr;
    }

Usage Example

示例#1
0
 /**
  * @param array $expression
  * @param Call $call
  * @param CompilationContext $context
  * @return bool|CompiledExpression|mixed
  * @throws CompilerException
  */
 public function optimize(array $expression, Call $call, CompilationContext $context)
 {
     if (!isset($expression['parameters'])) {
         return false;
     }
     if (count($expression['parameters']) != 2) {
         return false;
     }
     /**
      * Process the expected symbol to be returned
      */
     $call->processExpectedReturn($context);
     $symbolVariable = $call->getSymbolVariable();
     if ($symbolVariable->isNotVariableAndString()) {
         throw new CompilerException("Returned values by functions can only be assigned to variant variables", $expression);
     }
     if ($expression['parameters'][0]['parameter']['type'] == 'string') {
         $str = Utils::addSlashes($expression['parameters'][0]['parameter']['value']);
         unset($expression['parameters'][0]);
     }
     if ($call->mustInitSymbolVariable()) {
         $symbolVariable->initVariant($context);
     }
     $resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);
     $context->headersManager->add('kernel/string');
     $symbolVariable->setDynamicTypes('string');
     if (isset($str)) {
         $context->codePrinter->output('zephir_fast_join_str(' . $symbolVariable->getName() . ', SL("' . $str . '"), ' . $resolvedParams[0] . ' TSRMLS_CC);');
         return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
     }
     $context->codePrinter->output('zephir_fast_join(' . $symbolVariable->getName() . ', ' . $resolvedParams[0] . ', ' . $resolvedParams[1] . ' TSRMLS_CC);');
     return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
 }
All Usage Examples Of Zephir\Utils::addSlashes