0

I am trying to convert a strint composed of "standard" upper and lower case letters to small capitals, in PHP. There is no information about small caps in the php.net docs.

Small capitals are, in my understanding, this thing :

Hᴇʟʟᴏ ᴛʜᴇʀᴇ

(generated with this site : https://fsymbols.com/generators/smallcaps/)

For instance, here is the small cap version of a t : http://www.fileformat.info/info/unicode/char/1d1b/index.htm

I searched the web for a longtime and found nothin in PHP. I know that CSS let you do that by using

font-variant: small-caps;

But I need to do this on the server side for what I do. Is this possible in PHP ?

EDIT: To complete my question, I am trying to generate plain text. So no HTML, images or CSS is possible in my case.

EDIT 2: On the website linked, a Javascript function is used to convert the text

Here is the code :

function encool() {
    var _0xce74x20 = location[_0x804c[82]],
        _0xce74x21;
    if (_0xce74x20[_0x804c[84]](_0x804c[83]) == -1 && _0xce74x20[_0x804c[84]](_0x804c[85]) == -1 && _0xce74x20[_0x804c[84]](_0x804c[86]) == -1 && _0xce74x20[_0x804c[84]](_0x804c[87]) == -1 && _0xce74x20[_0x804c[84]](_0x804c[88]) == -1) {
        _0xce74x21 = document[_0x804c[91]][_0x804c[90]][_0x804c[89]]
    } else {
        _0xce74x21 = change(decomposeAString(document[_0x804c[91]][_0x804c[90]][_0x804c[89]]))
    };
    document[_0x804c[91]][_0x804c[92]][_0x804c[89]] = _0xce74x21
}

Pretty sure he is using a character mapping. I will look into that and post the solution if I find it.

Gfra54
  • 440
  • 5
  • 8
  • Do you want to show the text as an *image* or just change the style of a specific element? Can't you add a class or id to the element and use CSS rules for that class or id? Or even add a `style` attribute to the element? – Some programmer dude Feb 05 '18 at 09:49
  • _But I need to do this on the server side for what I do_ doesn't help much. Just print a style tag around your output perhaps or give it a css class? – huysentruitw Feb 05 '18 at 09:49
  • 1
    You could split up every character and change it to the unicode version of the same character. Seems like a lot of effort when a styling rule (as you showed) would be the better approach. – IsThisJavascript Feb 05 '18 at 09:50
  • 1
    Apparently the solution would be have a map for each character to its small caps pendant in unicode and then apply that map to each string. – Erik Kalkoken Feb 05 '18 at 09:54
  • Plain text have no formatting. It's simply not possible for you to do as it's a pure presentational issue. Styling or fonts have to be handled by the presenter of the plain text. – Some programmer dude Feb 05 '18 at 09:55
  • Yes, sorry for the lack of precision: I just edited my question to specify that I am trying to generate plain text. So no images, css or hmlt is possible in my case. – Gfra54 Feb 05 '18 at 09:55
  • @Someprogrammerdude yet [this guy](https://fsymbols.com/generators/smallcaps/) manage to do it with a JS function (which is too complicated for me to understand completely) using the "unicode mapping" technique mentionned here (I guess) – Gfra54 Feb 05 '18 at 10:00
  • Note that the mapping to Unicode characters (discussed in comments and in two answers) relies on the receiver to be able to handle it. The big browsers should be able to do it, but beyond that Unicode support is, still today, often lacking. – Some programmer dude Feb 05 '18 at 10:13
  • 1
    Author here. Yes, on FSymbols I'm first decomposing accented chars (like ș) into accent signs + plain letters - a feature you probably didn't notice - and then changing lowercase letters into their smallcaps analogues. You can use the tool itself to compile the transformation table. – Íhor Mé Feb 05 '18 at 10:36

3 Answers3

2

So you want to convert a text containing lower and upper letters into small capitals using UNICODE.

For that you need a mapping table, that maps each character to its small capitals pendant in UNICODE. Use mb_convert_encoding() for the conversion in PHP.

See this example from the PHP documentation on how to use a custom mapping table.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
1

Map each character and then loop over your string.

<?php
//Initialize the map
$map = array(
    "A"=>"ᴀ",
    "B"=>"ʙ",
    "C"=>"ᴄ",
    "D"=>"ᴅ"
    //etc
    );

function convertToSmall($string,$map){   
    //Our string to return
    $return = "";
    //You can replace length + the for loop for an explode + foreach
    $length = strlen($string);
    for($i = 0; $i<$length; $i++){
        //set our input character
        $input = strtoupper($string[$i]);
        //check if its in the map
        if(isset($map[$input])){
            $input = $map[$input];
        } 
        //write to our output
        $return .= $input;

    }

    return $return;

}

print_r(convertToSmall("aa bc da",$map));    

Output of this:
ᴀᴀ ʙᴄ ᴅᴀ

3v4l link

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
  • This is interesting. And quite simple (I will need to get rid of all accentuated chars before this, but this is a good starting point !) – Gfra54 Feb 05 '18 at 10:10
1

Ok here is the solution I came up with, but it is not very complete because even if it fits my needs. I needed this to convert small text (category names) so it is ok for me.

function convertToSmallCaps($string) {
    // standar capitals
    $caps = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    // small capitals
    $smallCaps = array('ᴀ', 'ʙ', 'ᴄ', 'ᴅ', 'ᴇ', 'ꜰ', 'ɢ', 'ʜ', 'ɪ', 'ᴊ', 'ᴋ', 'ʟ', 'ᴍ', 'ɴ', 'ᴏ', 'ᴘ', 'ǫ', 'ʀ', 's', 'ᴛ', 'ᴜ', 'ᴠ', 'ᴡ', 'x', 'ʏ', 'ᴢ');

    // remove all chars except [a-z] and - (replacing dashes with spaces)
    $sanitized_string = str_replace('-',' ',sanitize_title($string));

    // convert to uppercase
    $sanitized_string = mb_strtoupper($string);

    $length = strlen($sanitized_string);
    $output_string='';
    for($i = 0; $i<$length; $i++){
        $char = $sanitized_string[$i];
        // If the letter exsist in small capitals
        $letter_position = array_search($char,$caps);
        if(is_numeric($letter_position) && isset($smallCaps[$letter_position])) {
            // We append it to the ouput string
            $output_string.=$smallCaps[$letter_position];

        } else {
            // or else we append the original char to the output string
            $output_string.=$char;
        }
    }
    // return the converted string
    return $output_string;
}

It basically maps all chars in the string from caps to small caps

Problems :

  1. it is wordpress dependent. I used the sanitize_title function from wordpress, which "slugifies" the string (removing all chars except [a-z] and -)
  2. If your text has a lot of diacritics or is a composed of numbers and non latin chars, or longer than a category name, the result may be a bit weird
  3. some of the letters (the x or s or q) are not actual small caps (because they don't exist, I guess ?) so the result can be confusing on some display.

I will try to improve this in the future if my need change

Gfra54
  • 440
  • 5
  • 8