如何在PHP中将字符串转换为小型大写字母?

时间:2018-02-05 09:45:47

标签: php string-conversion smallcaps

我试图在PHP中将由“标准”大写和小写字母组成的strint转换为小写字母。在php.net文档中没有关于小型大写的信息。

根据我的理解,小资本是这样的:

  

Hᴇʟʟᴏᴇʟʟᴏ

(使用此网站生成:https://fsymbols.com/generators/smallcaps/

例如,以下是t的小型版本:http://www.fileformat.info/info/unicode/char/1d1b/index.htm

我在网上搜索了很长时间,发现在PHP中没什么用。我知道CSS允许你使用

来做到这一点
font-variant: small-caps;

但我需要在服务器端执行此操作。这可能在PHP吗?

编辑:要完成我的问题,我正在尝试生成纯文本。因此,在我的情况下,不可能有HTML,图像或CSS。

编辑2:在链接的网站上,使用Javascript函数转换文本

以下是代码:

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
}

很确定他正在使用角色映射。如果我找到它,我会调查并发布解决方案。

3 个答案:

答案 0 :(得分:2)

因此,您希望使用UNICODE将包含较低和较高字母的文本转换为小写字母。

为此您需要一个映射表,将每个字符映射到UNICODE中的小型大写字母。使用mb_convert_encoding()进行PHP转换。

请参阅PHP文档中的this example,了解如何使用自定义映射表。

答案 1 :(得分:1)

映射每个字符然后循环遍历字符串。

Graphics.DrawImage

输出:
<?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));

3v4l link

答案 2 :(得分:0)

好的,这是我提出的解决方案,但它不是很完整,因为即使它符合我的需要。我需要这个来转换小文本(类别名称),所以对我来说没问题。

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;
}

它基本上将字符串中的所有字符从大写字母映射到小字幕

问题:

  1. 它依赖于wordpress。我使用了wordpress中的sanitize_title函数,其中&#34; slugifies&#34;字符串(删除除[a-z]-之外的所有字符)
  2. 如果您的文字有很多变音符号或由数字和非拉丁字符组成,或者比类别名称更长,结果可能有点奇怪
  3. 有些字母(x或s或q)不是实际的小字母(因为它们不存在,我猜?)所以结果可能会在某些显示屏上混淆。
  4. 如果我需要改变,我会在将来尝试改进这个

相关问题