1

I have the following code in php

$test = "\151\163\142\156";
echo utf8_decode($test);
var_dump($test);

and i get the following result:

isbn
string(4) "isbn"

I get some text from a txt file that has the \151\163\142\156 text

$all_text = file_get_contents('test.txt');
var_dump($all_text);

result:

string(16) "\151\163\142\156"

I have the following questions:

  1. how can i utf8 decode the second text so i get the isbn result?

  2. how can i encode the isbn to get \151\163\142\156 ?

EDIT

(from comments)

I tried everything with iconv and encode but nothing worked. The text from the .txt file is string(16) and not string(4) so i can encode it. The txt file is saved from sublime with Western (ISO 8859-1) encoding

8
  • 3
    Seriously? utf8_decode($all_text);? utf8_encode('WHAT YOU NEED')? Commented Jun 21, 2016 at 20:13
  • that does not work. I get the same text and not the encoded one. as you can see the first one $test is string(4) and the second is string(16) Commented Jun 21, 2016 at 20:18
  • Do you decode second string? Show output of DECODED $all_text Commented Jun 21, 2016 at 20:19
  • $all_text_utf8_decoded = utf8_decode(file_get_contents('test.txt')); and php.net/manual/de/function.utf8-encode.php Commented Jun 21, 2016 at 20:20
  • does that matter, as file_get_contents is a reference point for the file rather than a container for the data itself? Commented Jun 21, 2016 at 21:20

2 Answers 2

1

This has absolutely nothing to do with UTF-8 encoding. Forget about that part entirely. utf8_decode doesn't do anything in your code. iconv is entirely unrelated.

It has to do with PHP string literal interpretation. The \... in "\151\163\142\156" is a special PHP string literal escape sequence:

\[0-7]{1,3}
the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000")

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

Which very easily explains why it works when written in a PHP string literal, and doesn't work when reading from an outside source (because the external text read through file_get_contents is not being interpreted as PHP code). Simply do echo "\151\163\142\156" and you'll see "isbn" without any other conversions necessary.

To manually convert the individual escape sequences in the string \151\163\142\156 to their character equivalents (really: their byte equivalents):

$string = '\151\163\142\156';  // note: single quotes cause no iterpretation
echo preg_replace_callback('/\\\\([0-7]{1,3})/', function ($m) {
    return chr(octdec($m[1]));
}, $string)
// isbn

stripcslashes happens to include this functionality, but it also does a whole lot of other things which may be undesired.

The other way around:

$string = 'isbn';
preg_replace_callback('/./', function ($m) {
    return '\\' . decoct(ord($m[0]));
}, $string)
// \151\163\142\156
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That's what i wanted to do.
1

Try using stripcslashes :

<?php

$test = "\151\163\142\156";
echo utf8_decode( $test );                         // "isbn"
var_dump( $test );

echo "<br/><br/><br/>";

$all_text = file_get_contents( "test.txt" );
echo utf8_decode( $all_text ) .                    // "\151\163\142\156"
     "<br/>" .
     utf8_decode( stripcslashes( $all_text ) );    // "isbn"
var_dump( stripcslashes( $all_text ) );

?>

Tested with this file :

This is some text :

\151\163\142\156

And this is more text!!!

Next is how to convert chars to codes :

<?php
$test = "isbn";
$coded = "";
for ( $i = 0; $i < strlen( $test ); $i++ ) // PROCESS EACH CHAR IN STRING.
  $coded .= "\\" . decoct( ord( $test[ $i ] ) ); // CHAR CODE TO OCTAL.

echo $coded .                           // "\151\163\142\156"
     "<br/>" .
     stripcslashes( $coded );           // "isbn".
?>

Let's make it more general with a function that we can call anywhere :

<?php
function code_string ( $s )
{ $coded = "";
  for ( $i = 0; $i < strlen( $s ); $i++ )
    $coded .= "\\" . decoct( ord( $s[ $i ] ) );
  return $coded;
}

$x = code_string( "isbn" );
echo $x .                           // "\151\163\142\156"
     "<br/>" .
     stripcslashes( $x );           // "isbn".
?>

4 Comments

You can get rid of utf8_decode entirely, it doesn't do anything here.
@deceze, I agree, but the OP seems to like it (it's possible that the text file will come with weird characters).
I thought you were trying to educate the OP about what they're doing wrong and/or are misunderstanding...!? :-P
Thank you guys. Both yours and @deceze was a great solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.