zephir
zephir copied to clipboard
Get char from a string
$test = "test";
$t = $test[1];
var_dump($t);
string(1) "e"
zephir
char t;
string test = "test";
let t = test[1];
var_dump(t);
ZEPHIR_INIT_VAR(&test);
ZVAL_STRING(&test, "test");
t = ZEPHIR_STRING_OFFSET(&test, 1);
t = t;
ZEPHIR_INIT_VAR(&_10);
ZEPHIR_INIT_NVAR(&_10);
ZVAL_LONG(&_10, t);
zephir_var_dump(&_10);
int(101)
A cast char to string workaround:
char t;
string test = "test";
let t = test[1];
var s;
let s = (string) t;
but it's painful if I want to do ord(data[0])
, we could play with chr()
, but @sergeyklay maybe there is a better way, is it related to https://github.com/phalcon/zephir/issues/1988?
I think it works if we do it in the loop, but I have to double-check:
int i;
char c;
string test = "test";
for i, c in test {
var_dump(c, test[i]);
}
@mruz Thank you for the bug report. I'll try to sort out
And yes, this is related to #1988