Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用preg_replace仅检查字母数字和其他语言字母?_Php_Preg Replace - Fatal编程技术网

Php 如何使用preg_replace仅检查字母数字和其他语言字母?

Php 如何使用preg_replace仅检查字母数字和其他语言字母?,php,preg-replace,Php,Preg Replace,我需要创建一个搜索引擎优化友好的字符串只从字母数字和我的母语字符。这是僧伽罗 我期望的字符串应该是这样的: $myString = "this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන"; 我正在使用一个函数来创建这样的字符串。该函数如下所示: function seoUrl($string) { //Lower case everything $string = strtolower($string);

我需要创建一个搜索引擎优化友好的字符串只从字母数字和我的母语字符。这是僧伽罗

我期望的字符串应该是这样的:

$myString = "this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන";
我正在使用一个函数来创建这样的字符串。该函数如下所示:

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}
此函数仅适用于英文字符和上述字符串的输出,如下所示:

$title = seoUrl("this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන");
echo $title; // this-is-a-
有人能告诉我如何修改上述函数以获得我的所有字符(包括我的母语字符)吗


希望有人能帮助我。多谢各位

您使用多字节编码。preg_replace不适用于多字节编码。如果使用多字节编码,则应使用函数。preg_replace不适用于多字节编码。您应该对unicode使用函数对unicode使用
/u
标志,对字母使用
\pL
,对数字使用
\pN

编辑:由于某些多字节字符,mb_ereg_replace是不错的选择:

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = mb_ereg_replace("[^\x0D-\x0E\w\s-]", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}
$title = seoUrl("this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන");
echo $title;
输出:

this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන

unicode使用
/u
标志,字母使用
\pL
,数字使用
\pN

编辑:由于某些多字节字符,mb_ereg_replace是不错的选择:

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = mb_ereg_replace("[^\x0D-\x0E\w\s-]", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}
$title = seoUrl("this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන");
echo $title;
输出:

this-is-a-දහසක්-බාධක-දුක්-කම්කටොලු-මැදින්-ලෝකය-දිනන්නට-වෙර-දරන

但僧伽罗文字中缺少一些部分。请仔细观察这两个字符串,你会注意到它们的区别。但僧伽罗文字中缺少一些部分。请仔细看两条线,你会注意到它们的区别。