1

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?

0

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

My apologies, I wasn't clear in my question. Ideally I shouldn't print by character, I needed to print a composited string. Is that not possible in this case?
Well.. not ideal.. we have a function that literally uses input in this form "\155\155" and it works with plain echo. For certain reasons I can't change that. It's echo $str; and $str needs to be composited dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.