0

Is There a way to print special characters in PHP using only source code with ascii characters?

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

And in PHP? How can I use it?

I don't want to include special chars in my source code.

danilo
  • 7,680
  • 7
  • 43
  • 46
  • 2
    PHP uses the same string syntax all the other coding languages do. So it would be the same. –  Apr 23 '18 at 21:15
  • `u00e1` is unicode, supported by many devices, this is not a php specific issue, it depends what is is displaying the strings you output via php –  Apr 23 '18 at 21:16
  • @TyQ. `nl2br` is other function. hsc one is for `
    ` to `<br>` conversion
    – vp_arth Apr 23 '18 at 21:21
  • @TyQ., you are wrong also about same syntax. "\u{2202}" was introduced in php7 only – vp_arth Apr 23 '18 at 21:23
  • 1
    @vp_arth Really? I didn't know that. I always use PHP 7 so I'm not familiar with older versions and what they did or did not support. Nice to know, I guess. (I removed downvote since I can see where OP is coming from now) –  Apr 23 '18 at 21:24

1 Answers1

1

I found 3 ways for this.

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

A good explanation in portuguese: https://pt.stackoverflow.com/questions/293500/escrevendo-c%C3%B3digo-em-php-sem-caracteres-especiais

Sintax added only in PHP7:

\u{[0-9A-Fa-f]+} 
the sequence of characters matching the regular expression is a Unicode codepoint.
which will be output to the string as that codepoint's UTF-8 representation

examples:

<?php
  echo "\u{00e1}\n";
  echo "\u{2202}\n";
  echo "\u{aa}\n";
  echo "\u{0000aa}\n"; 
  echo "\u{9999}\n";

Sintax for PHP7 and old PHP versions:

\x[0-9A-Fa-f]{1,2}
the sequence of characters matching the regular expression,
is a character in hexadecimal notation

examples:

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

Using int to binary convertion functions:

<?php
  printf('%c%c', 0xC3, 0xA1);  
  echo chr(0xC3) . chr(0xA1);  

printf() Extended Unicode Characters?

http://phptester.net/

danilo
  • 7,680
  • 7
  • 43
  • 46