Writing PHP code without special characters

Asked

Viewed 272 times

3

There is a way to print special characters in PHP using ascii code only?

For example, in javascript, we can use u00e1 in the middle of the text. In Java, we can use u2202 for example.

What about PHP? How do I use it?

I don’t want to include special characters in my code.

  • You want to know how to print characters ?

  • 2

    In addition to Guilherme’s answer, you can use Chr( ) to represent any byte (or sequence of them, concatenating) by making the combination you want.

1 answer

5


What’s new in PHP7:

There is support for Unicode code escape syntax, for example:

<?php
echo "\u{00e1}\n";
echo "\u{2202}\n";
echo "\u{aa}\n";
echo "\u{0000aa}\n"; //o mesmo que o anterior mas com zeros a esquerda
echo "\u{9999}\n";

Will be printed as:

á
∂
ª
ª
香

Example in the ideone: https://ideone.com/2Tcsed

Note 1: The \n is only for line breaking is just to separate the echos, in HTML use <br>

Note 2: Only double quotes support this, single quotes like this echo '\u{aa}'; won’t work


Escape syntax in PHP5

In PHP before 7 (ie 5) there was (and still exists) this syntax:

\x[0-9A-Fa-f]{1,2}

That uses hexadecimal notation and is limited to 1 or 2 digits after the \x and just like Unicode (\u{[0-9A-Fa-f]+}) should also be used in double quote notations.

Then to write a Unicode character it will be necessary to use two or more times the \x (since single characters are formed like this), for example \xc3\xa1 would amount to \u{00e1}, example both print á:

<?php
echo "\xc3\xa1\n";
echo "\u{00e1}\n";

Comparing both:

if ("\xc3\xa1" === "\u{00e1}") {
    echo 'São iguais';
} else {
    echo 'São diferentes';
}

Will display São iguais


Alternative

There is also the function chr(...) or even sprintf(...) (or even with printf(...)), for example:

<?php

$caracterChr = chr(27);

$caracterPrintf = sprintf('%c', 27);

var_dump($caracterChr, $caracterPrintf);

Comparing both:

if ($caracterChr === $caracterPrintf) {
    echo 'São iguais';
} else {
    echo 'São diferentes';
}

See the example in ideone: https://ideone.com/FJnGJp

  • 2

    Very good!! I didn’t know! =)

  • 1

    @Andreicoelho took advantage and put the supported syntax in PHP5 ;)

  • The reply was complete!

  • 1

    Only the chr( ), that serves to represent anything (including UTF). cc @Andreicoelho

  • 1

    @Bacco I thought about talking about this, but 2 situations, 1. Andrei’s answer already spoke and until then he had not deleted, 2. the question is to write as it is in Java and Javascript, so I thought Chr would be something "bureaucratic", but I will think of something to quote or write.

  • yes. I even thought about restoring the answer after the @Bacco comment. But I thought you could put it in if you thought it was necessary. But, as you said yourself, it would be more of a quote. A complement.

  • I like the idea of restoring, because you can work separately each thing (but you have to give some "tapas" in the restored, maybe with an example in UTF-8) - I just didn’t post one just because I saw Andrei’s

  • @Bacco added a brief explanation about Chr and sprintf.

  • 1

    @Guilhermenascimento, thank you very much, full answer and very well explained, fantastic.

  • @Guilhermenascimento only lacked one detail, Chr and sprintf using %c use decimal to represent the binary, so you have to use a string, to represent UTF-8 for example: printf('%c%c', 0xC3, 0xA1); echo Chr(0xC3) . Chr(0xA1);

  • @Danilo yes that’s right, it’s like with the \x27, is also in chain :[ ... only the \u has support for Unicode in fact. That’s why I didn’t even mention Chr before, because I knew it wasn’t what you wanted, I added it in response to requests from other users

  • 1

    @Guilhermenascimento, thank you, yes what I was looking for was for.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.