GVKun编程网logo

PHP数组排序和与波斯语字母表的兼容性(php将数组以字母先后顺序排序)

22

本文将分享PHP数组排序和与波斯语字母表的兼容性的详细内容,并且还将对php将数组以字母先后顺序排序进行详尽解释,此外,我们还将为大家带来关于086-PHP数组按数字排序和按字母排序、JSON文件无法

本文将分享PHP数组排序和与波斯语字母表的兼容性的详细内容,并且还将对php将数组以字母先后顺序排序进行详尽解释,此外,我们还将为大家带来关于086-PHP数组按数字排序和按字母排序、JSON文件无法正确显示阿拉伯语/波斯语字符、php – 使用laravel elequent在MySQL中搜索波斯语字符串、php – 使用preg_match来检测字符串中的波斯语(波斯语)字符的相关知识,希望对你有所帮助。

本文目录一览:

PHP数组排序和与波斯语字母表的兼容性(php将数组以字母先后顺序排序)

PHP数组排序和与波斯语字母表的兼容性(php将数组以字母先后顺序排序)

我试图先按照它的值对数组进行排序,然后按它的键进行排序,但是对于波斯语字符,PHP并不是很好.

波斯语字母表类似于阿拉伯字母,除了一些额外的字符,如’گچپژک’和PHP在波斯语字母表中排序阿拉伯字母方面做得很好,但其余的不在他们的顺序中.

例如

$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);
@H_301_11@

将按正确的字母顺序创建一个包含所有波斯语字母的数组($arr).如果我将它洗牌并使用如下的asort函数:

shuffle($arr);
asort($arr);
var_dump($arr);
@H_301_11@

它会像这样结束:

    array
        2 => string 'ا'
        1 => string 'ب'
        22 => string 'ت'
        29 => string 'ث'
        20 => string 'ج'
        12 => string 'ح'
        21 => string 'خ'
        18 => string 'د'
        6 => string 'ذ'
        3 => string 'ر'
        27 => string 'ز'
        17 => string 'ص'
        11 => string 'ض'
        25 => string 'ط'
        5 => string 'ظ'
        16 => string 'ع'
        8 => string 'غ'
        26 => string 'ف'
        14 => string 'ق'
        9 => string 'ل'
        0 => string 'م'
        7 => string 'ن'
        10 => string 'ه'
        28 => string 'و'
        24 => string 'پ'
        23 => string 'چ'
        13 => string 'ژ'
        19 => string 'ک'
        4 => string 'گ'
        15 => string 'ی'
@H_301_11@

这是错的!

第24项应在第1项之后,第23项应在20之后,依此类推.

如何编写与PHP自己的排序函数类似的函数?或许有一种方法可以让PHP函数适用于波斯语字符?

解决方法:

我编写了以下函数来返回任何给定字符的UTF-8代码点:

function utf8_ord($str) {
    $str = (string) $str;
    $ord = ord($str);
    $ord_b = decbin($ord);

    if (strlen($ord_b) <= 7) 
      return $ord;
    $len = strlen(strstr($ord_b, "0", true));

    if ($len < 2 || $len > 4 || strlen($str) < $len) 
      return false;
    $val = substr($ord_b, $len + 1);

    for ($i = 1; $i < $len; $i++) {
        $ord_b = decbin(ord($str[$i]));
        if ($ord_b[0].$ord_b[1] != "10") 
          return false;
        $val. = substr($ord_b, 2);
    }
    $val = bindec($val);
    return (($val > 0x10FFFF) ? null : $val);
}
@H_301_11@

现在让我们找出数组中字符的UTF-8代码点:

$str = 'ا ب پ ت ث ج چ ح خ د ذ ر ز ژ ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی';
$arr = explode(' ', $str);
print_r(array_map("utf8_ord", $arr));
@H_301_11@

输出将是:

Array
(
    [0] => 1575
    [1] => 1576
    [2] => 1662
    [3] => 1578
    [4] => 1579
    [5] => 1580
    [6] => 1670
    [7] => 1581
    [8] => 1582
    [9] => 1583
    [10] => 1584
    [11] => 1585
    [12] => 1586
    [13] => 1688
    [14] => 1589
    [15] => 1590
    [16] => 1591
    [17] => 1592
    [18] => 1593
    [19] => 1594
    [20] => 1601
    [21] => 1602
    [22] => 1705
    [23] => 1711
    [24] => 1604
    [25] => 1605
    [26] => 1606
    [27] => 1608
    [28] => 1607
    [29] => 1740
)
@H_301_11@

它清楚地表明字符的顺序不正确,需要进行排序.我不知道波斯语,所以我无法确定UTF-8波斯语字母表是否有错.但我只能说PHP正在正常工作.

086-PHP数组按数字排序和按字母排序

086-PHP数组按数字排序和按字母排序

<?php
    $arr=array(2,54,167,''a'',''A'',''12'');        //定义一个数组
    echo ''数组排序之前的信息:<br />'';
    print_r($arr);        //输出数组的信息
    echo ''<br />数组普通排序之后的信息:<br />'';
    sort($arr);            //对数组进行排序
    print_r($arr);
    echo ''<br />数组作为数字排序之后的信息:<br />'';
    sort($arr,SORT_NUMERIC);        //将数组的元素转换为数字进行排序
    print_r($arr);
    echo ''<br />数组作为字符排序之后的信息:<br />'';
    sort($arr,SORT_STRING);        //将数组的元素转换为字符进行排序
    print_r($arr);
?>

 

JSON文件无法正确显示阿拉伯语/波斯语字符

JSON文件无法正确显示阿拉伯语/波斯语字符

这按预期工作。它称为Unicode,是一种标准的字符编码方式,通常在编程中用于表示标准ASCII字符集以外的字符。

JSON:https://www.json.org/json-en.html-“字符串是零个或多个Unicode字符的序列,使用反斜杠转义符将其括在双引号中。”

Unicode:https://en.wikipedia.org/wiki/Unicode

php – 使用laravel elequent在MySQL中搜索波斯语字符串

php – 使用laravel elequent在MySQL中搜索波斯语字符串

在我的laravel中,为了搜索产品标题列,我使用以下代码:

$products->where('title', 'like', '%' . $request->title . '%');

title列是一个字符串列,存储在其中的数据是波斯语.此外,数据库排序规则是UTF8_general_ci.然而,当我搜索某些标题时,有些标题没有.我需要结果来查找标题列中包含$request->标题的每个产品.

你能帮助我吗?

解决方法:

将归类UTF8_general_ci更改为latin1_swedish_ci

Collations have these general characteristics:

Two different character sets cannot have the same collation.

Each character set has one collation that is the default collation. For example, the default collation for latin1 is latin1_swedish_ci. The output for SHOW CHaraCTER SET indicates which collation is the default for each displayed character set.

There is a convention for collation names: They start with the name of the character set with which they are associated, they usually include a language name, and they end with _ci (case insensitive), _cs (case sensitive), or _bin (binary).

In cases where a character set has multiple collations, it might not be clear which collation is most suitable for a given application. To avoid choosing the wrong collation, it can be helpful to perform some comparisons with representative data values to make sure that a given collation sorts values the way you expect.

参考here

php – 使用preg_match来检测字符串中的波斯语(波斯语)字符

php – 使用preg_match来检测字符串中的波斯语(波斯语)字符

我正在尝试从服务器端验证表单数据.
我的兴趣是用户只需填写波斯字符的形式.

我正在使用这段代码:

$name=trim($_POST['name']);
$name= MysqL_real_escape_string($name);
if (preg_match('/^[\u0600-\u06FF]+$/',str_replace("\\\\","",$name))){$err.= "Please use Persian characters!";}

但它不工作!

这是一个警告:

Warning: preg_match() [function.preg-match]: Compilation Failed: PCRE does not support \L,\l,\N,\U,or \u at offset 3 in C:\xampp\htdocs\site\form.PHP on line 38

我能做什么?

这个“应该”工作

**在打开后添加了^ [从排除阿拉伯/波斯字符…

if (preg_match('/^[^\x{600}-\x{6FF}]+$/u',$name)))

今天关于PHP数组排序和与波斯语字母表的兼容性php将数组以字母先后顺序排序的分享就到这里,希望大家有所收获,若想了解更多关于086-PHP数组按数字排序和按字母排序、JSON文件无法正确显示阿拉伯语/波斯语字符、php – 使用laravel elequent在MySQL中搜索波斯语字符串、php – 使用preg_match来检测字符串中的波斯语(波斯语)字符等相关知识,可以在本站进行查询。

本文标签: