简体   繁体   中英

Echo Dynamic ascii string in PHP

I have a dynamic value $input that represents a character in ascii. But somehow I just can't print it out correctly.

$str= "\155\155";
echo 'value is '.$str;

$input = 155;
$num= "\\".$input."\\".$input;
echo 'another value '.$num;

The first line will be "mm" But the second line is "\\155\\155" Is there some conversion I am leaving out?

Yep. 155 is octal value to m.

Check it out:

$str= "\155\155";
echo 'value is '.$str;

$input = 155;
$num = octdec($input);
$num = chr($num);
echo ' another value from octal '.$num;

$input = 109;
$num = chr($input);
echo ' another value from decimal '.$num;

I'm not sure if you can do it straightforward. An easy way to reach what you want can be something like this:

$input = 155;
$num= "\\".$input."\\".$input;
$num = str_replace("\\".$input, chr(octdec($input)), $num);
echo 'another value '.$num;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM