private static function parseString($str)
    {
        if ($str[0] !== '"' || $str[strlen($str) - 1] !== '"') {
            throw new Exception("Invalid string delimiters");
        }

        $result = '';
        $start = str_split(substr($str, 1, -1), 1);
        $escaped = false;
        $data = null;

        foreach ($start as $chr) {
            if ($escaped === 'yes') {
                $escaped = false;
                switch ($chr) {
                    case 'n':  $result .= "\n"; break;
                    case 'r':  $result .= "\r"; break;
                    case 't':  $result .= "\t"; break;
                    case 'v':  $result .= "\v"; break;
                    case 'e':  $result .= "\e"; break;
                    case 'f':  $result .= "\f"; break;
                    case '\\': $result .= "\\"; break;
                    case '$':  $result .= "\$"; break;
                    case '"':  $result .= "\""; break;
                    case 'x':
                        $escaped = 'hex';
                        $data = '0x';
                        break;
                    default:
                        if (ctype_digit($chr) && (int)$chr < 8) {
                            $escaped = 'oct';
                            $data = $chr;
                        } else {
                            $result .= "\\" . $chr;
                        }
                        break;
                }
            } else if ($escaped === 'hex' && ctype_xdigit($chr)) {
                $data .= $chr;
                if (strlen($data) === 2) {
                    $escaped = false;
                }
            } else if ($escaped === 'oct' && ctype_digit($chr) && (int)$chr < 8) {
                $data .= $chr;
                if (strlen($data) === 3) {
                    $escaped = false;
                }
            } else {
                if ($data !== null || $escaped === 'hex' || $escaped === 'oct') {
                    if (substr($data, 0, 2) === '0x') {
                        if (strlen($data) === 2) {
                            $result .= "\\x";
                        } else {
                            $result .= chr(hexdec($data));
                        }
                    } else {
                        $result .= chr(octdec($data));
                    }
                    $data = null;
                }

                if ($chr === '\\') {
                    $escaped = 'yes';
                } else if ($chr === '"') {
                    throw new Exception("Unescaped string delimiter inside string");
                } else {
                    $result .= $chr;
                }
            }
        }
        if ($escaped !== false) {
            throw new Exception("Unfinished escape sequence");
        }
        return $result;
    }
private static function parseString($str)
    {
        if ($str[0] !== '"' || $str[strlen($str) - 1] !== '"') {
            throw new Exception("Invalid string delimiters");
        }

        $result = '';
        $start = str_split(substr($str, 1, -1), 1);
        $escaped = false;
        $data = null;

        foreach ($start as $chr) {
            if ($escaped === 'yes') {
                $escaped = false;
                switch ($chr) {
                    case 'n':  $result .= "\n"; break;
                    case 'r':  $result .= "\r"; break;
                    case 't':  $result .= "\t"; break;
                    case 'v':  $result .= "\v"; break;
                    case 'e':  $result .= "\e"; break;
                    case 'f':  $result .= "\f"; break;
                    case '\\': $result .= "\\"; break;
                    case '$':  $result .= "\$"; break;
                    case '"':  $result .= "\""; break;
                    case 'x':
                        $escaped = 'hex';
                        $data = '0x';
                        break;
                    default:
                        if (ctype_digit($chr) && (int)$chr < 8) {
                            $escaped = 'oct';
                            $data = $chr;
                        } else {
                            $result .= "\\" . $chr;
                        }
                        break;
                }
            } else if ($escaped === 'hex' && ctype_xdigit($chr)) {
                $data .= $chr;
                if (strlen($data) === 2) {
                    $escaped = false;
                }
            } else if ($escaped === 'oct' && ctype_digit($chr) && (int)$chr < 8) {
                $data .= $chr;
                if (strlen($data) === 3) {
                    $escaped = false;
                }
            } else {
                if ($data !== null || $escaped === 'hex' || $escaped === 'oct') {
                    if (substr($data, 0, 2) === '0x') {
                        if (strlen($data) === 2) {
                            $result .= "\\x";
                        } else {
                            $result .= chr(hexdec($data));
                        }
                    } else {
                        $result .= chr(octdec($data));
                    }
                    $data = null;
                }

                if ($chr === '\\') {
                    $escaped = 'yes';
                } else if ($chr === '"') {
                    throw new Exception("Unescaped string delimiter inside string");
                } else {
                    $result .= $chr;
                }
            }
        }
        if ($escaped !== false) {
            throw new Exception("Unfinished escape sequence");
        }
        return $result;
    }
private static function parseString ( $ str ) { if ( $ str [ 0 ] !== '"' || $ str [ strlen ( $ str ) - 1 ] !== '"' ) { throw new Exception ( "Invalid string delimiters" ) ; } $ result = '' ; $ start = str_split ( substr ( $ str , 1 , - 1 ) , 1 ) ; $ escaped = false ; $ data = null ; foreach ( $ start as $ chr ) { if ( $ escaped === 'yes' ) { $ escaped = false ; switch ( $ chr ) { case 'n' : $ result .= "\n" ; break ; case 'r' : $ result .= "\r" ; break ; case 't' : $ result .= "\t" ; break ; case 'v' : $ result .= "\v" ; break ; case 'e' : $ result .= "\e" ; break ; case 'f' : $ result .= "\f" ; break ; case '\\' : $ result .= "\\" ; break ; case '$' : $ result .= "\$" ; break ; case '"' : $ result .= "\"" ; break ; case 'x' : $ escaped = 'hex' ; $ data = '0x' ; break ; default : if ( ctype_digit ( $ chr ) && ( int ) $ chr < 8 ) { $ escaped = 'oct' ; $ data = $ chr ; } else { $ result .= "\\" . $ chr ; } break ; } } else if ( $ escaped === 'hex' && ctype_xdigit ( $ chr ) ) { $ data .= $ chr ; if ( strlen ( $ data ) === 2 ) { $ escaped = false ; } } else if ( $ escaped === 'oct' && ctype_digit ( $ chr ) && ( int ) $ chr < 8 ) { $ data .= $ chr ; if ( strlen ( $ data ) === 3 ) { $ escaped = false ; } } else { if ( $ data !== null || $ escaped === 'hex' || $ escaped === 'oct' ) { if ( substr ( $ data , 0 , 2 ) === '0x' ) { if ( strlen ( $ data ) === 2 ) { $ result .= "\\x" ; } else { $ result .= chr ( hexdec ( $ data ) ) ; } } else { $ result .= chr ( octdec ( $ data ) ) ; } $ data = null ; } if ( $ chr === '\\' ) { $ escaped = 'yes' ; } else if ( $ chr === '"' ) { throw new Exception ( "Unescaped string delimiter inside string" ) ; } else { $ result .= $ chr ; } } } if ( $ escaped !== false ) { throw new Exception ( "Unfinished escape sequence" ) ; } return $ result ; }
PHP String parsing without using eval.
@param string $str Unparsed double-quoted string with escape sequences.
@return string
PHP String parsing without using eval .