1

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.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347

1 Answers1

2

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • if so, then what RC posted would be a duplicate, *n'est-ce pas?* – Funk Forty Niner Sep 24 '15 at 15:26
  • I *wouldn't* do what the author did. All it takes it one errant open/save with a shoddy application to break that chunk of code. – Mr. Llama Sep 24 '15 at 15:27
  • 1
    @Mr.Llama, where I work a 'shoddy application' doing that would always have an effect on unittests, and a change would show up in a code review. If you work in a shop that's less diligent, I would agree with you... but we rely on process and tools and love embedded unicode =) – Evert Sep 24 '15 at 15:30
  • 1
    @Mr.Llama For web development, I'd consider not supporting Unicode a fatal, deal-breaking flaw in a program. *Especially in a text or HTML editor*. I'd absolutely hate having to write `caf\xc3\xa9` instead of `café` – roeland Sep 25 '15 at 00:49
  • @Evert Excactly was I was looking for, accepting the answer not your personal opionion ;) – k0pernikus Sep 25 '15 at 08:44