用PHP编写源代码而不需要特殊字符

时间:2018-04-23 21:13:17

标签: php

有没有办法只使用带有ascii字符的源代码在PHP中打印特殊字符?

例如,在javascript中,我们可以在文本中间使用\ u00e1 在Java中,我们可以使用\ u2202。

在PHP中?我该如何使用它?

我不想在我的源代码中包含特殊字符。

1 个答案:

答案 0 :(得分:1)

我找到了3种方法。

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

葡萄牙语的一个很好的解释:https://pt.stackoverflow.com/questions/293500/escrevendo-c%C3%B3digo-em-php-sem-caracteres-especiais

Sintax仅在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

的示例:

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

适用于PHP7和旧版PHP的Sintax:

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

的示例:

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

使用int到二进制转换函数:

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

printf() Extended Unicode Characters?

http://phptester.net/