我正在尝试在 PHP 中将由“标准”大写和小写字母组成的字符串转换为小写大写字母。 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
}
很确定他正在使用字符映射。如果我找到它,我会调查并发布解决方案。
因此您想使用 UNICODE 将包含大小写字母的文本转换为小写字母。
为此,您需要一个映射表,将每个字符映射到 UNICODE 中的小大写字母。在 PHP 中使用
mb_convert_encoding()
进行转换。
请参阅 PHP 文档中的此示例,了解如何使用自定义映射表。
映射每个字符,然后循环遍历字符串。
<?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));
此输出:
ᴀᴀ ʙᴄ ᴅᴀ
好的,这是我想出的解决方案,但它不是很完整,因为即使它符合我的需求。我需要它来转换小文本(类别名称),所以对我来说没问题。
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;
}
它基本上将字符串中的所有字符从大写字母映射到小型大写字母
问题:
sanitize_title
函数,它“slugizes”字符串(删除除 [a-z]
和 -
之外的所有字符)如果我的需求改变,我会在未来尝试改进
这是我的一个简单方法:
function small_caps($string)
{
$buf = "";
for ($i = 0; $i < strlen($string); $i++)
{
$char = substr($string, $i, 1);
if (ctype_lower($char))
$buf .= "<small>$char</small>";
else
$buf .= $char;
}
// remove redundancies
$buf = str_replace("</small><small>","",$buf);
return "<span style='text-transform:uppercase'>$buf</span>";
}