0

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

在我的理解中,小写字母是这个东西:

Hᴇʟʟᴏ ᴛʜᴇʀᴇ</p>

(由本网站生成:https ://fsymbols.com/generators/smallcaps/ )

例如,这里是 a 的小写版本thttp ://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
}

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

4

3 回答 3

2

因此,您想使用 UNICODE 将包含大小写字母的文本转换为小写字母。

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

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

于 2018-02-05T10:02:51.157 回答
1

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

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。我使用了sanitize_titlewordpress 中的函数,它“slugizes”了字符串(删除除[a-z]and之外的所有字符-
  2. 如果您的文本有很多变音符号,或者是由数字和非拉丁字符组成,或者比类别名称长,结果可能会有点奇怪
  3. 一些字母(x 或 s 或 q)不是实际的小型大写字母(因为它们不存在,我猜?)所以结果在某些显示上可能会令人困惑。

如果我的需要发生变化,我会在未来尝试改进这一点

于 2018-02-05T10:26:07.650 回答
1

映射每个字符,然后遍历您的字符串。

<?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链接

于 2018-02-05T10:06:03.277 回答