k0pernikus
k0pernikus

Reputation: 66509

How to use a unicode character without writing the actual unicode character?

I just stumbled over this code:

/**
 * @param $string
 * @return bool
 */
protected static function isRubel($string)
{
    $lString = strtolower($string);

    /**
     * @hint: The characters looking like "p" as in Petra are in fact instances of cyrillic "Er"!
     */

    return strpos($string, 'руб') !== false || strpos($string, 'р') !== false || strpos($lString, 'rub') !== false;
}

It was very nice of the author to provide a hint, that the character is not a p but a unicode symbol р; as I would have assumed it was a p.

Could this code be written differently in order to make it more clear that the character is a unicode symbol?

In a sense, write unicode representation without actually writing unicode character.

I know I could define a string constant CRYLLIC_ER = р, yet I was looking for a more general solution.

Upvotes: 1

Views: 106

Answers (1)

Evert
Evert

Reputation: 99553

From PHP 7 onwards you can use this syntax:

$str = "\u{####}";

Before that, you could use the UTF-8 byte values to make it dead obvious:

$str = "\x##\x##";

But personally I would do what the author did, just write it in a comment.

Upvotes: 2

Related Questions