PHP convert an octal characters to string
Asked Answered
S

3

5

Title is pretty much self explanatory...
How do I echo an octal string ?
I tried :

<?php
echo '\047\131\145\141\162\040\072\040\047'.'<br>';
echo decoct('\047\131\145\141\162\040\072\040\047').'<br>';
echo decoct('047').decoct('131').decoct('145').decoct('141').decoct('162').decoct('040').decoct('072'),decoct('040').decoct('047').'<br>';
?>

but nothing is working for me....
I'm quite sure that some small tweak is needed here but... which one?
Thanks!

Selfness answered 14/12, 2014 at 11:46 Comment(0)
D
6

Escape sequences are only processed inside double quoted strings, not single-quoted strings.

echo "\047\131\145\141\162\040\072\040\047".'<br>';
Depute answered 14/12, 2014 at 11:49 Comment(0)
L
5

This a backslash escaped string, so use stripcslashes() to un-escape, like this:

$escaped = '\047\131\145\141\162\040\072\040\047'.'<br>';
$unescaped = stripcslashes($escaped);
echo $unescaped;

Result:

'Year : '<br>
Lemmueu answered 13/4, 2015 at 13:5 Comment(0)
A
3

This may help

function convertOctalToCharacter($octal) {
    return chr(octdec($octal[1]));
}

For a mass-tokenizing of strings with octals, this regex-processing may become handy too:

$string = preg_replace_callback('/\\\\([0-7]{1,3})/', 'convertOctalToCharacter', $string);

Credits go to http://www.matthewratzloff.com/ blog post

Antihero answered 14/12, 2014 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.