1

这段代码:

setlocale(LC_ALL, 'pl_PL', 'pl', 'Polish_Poland.28592');
$result = mb_stripos("ĘÓĄŚŁŻŹĆŃ",'ęóąśłżźćń');

返回假;

如何解决?

PS当使用特殊字符时,此 stripos 返回 false不是正确答案。


更新:我做了一个测试:

function test() {
    $search = "zawór"; $searchlen=strlen($search);
    $opentag="<valve>"; $opentaglen=strlen($opentag);
    $closetag="</valve>"; $closetaglen=strlen($closetag);
    $test[0]['input']="test ZAWÓR test"; //normal test
    $test[1]['input']="X\nX\nX ZAWÓR X\nX\nX"; //white char test
    $test[2]['input']="<br> ZAWÓR <br>"; //html newline test
    $test[3]['input']="ĄąĄą ZAWÓR ĄąĄą"; //polish diacritical test
    $test[4]['input']="テスト ZAWÓR テスト"; //japanese katakana test
    foreach ($test as $key => $val) {
        $position = mb_stripos($val['input'],$search,0,'UTF-8');
        if($position!=false) {
            $output = $val['input'];
            $output = substr_replace($output, $opentag, $position, 0);
            $output = substr_replace($output, $closetag, $position+$opentaglen+$searchlen, 0);
            $test[$key]['output'] = $output;
        }
        else {
            $test[$key]['output'] = null;
        }
    }
    return $test;
}

火狐输出:

$test[0]['output'] == "test <valve>ZAWÓR</valve> test"        // ok
$test[1]['output'] == "X\nX\nX <valve>ZAWÓR</valve> X\nX\nX"  // ok
$test[2]['output'] == "<br> <valve>ZAWÓR</valve> <br>"        // ok
$test[3]['output'] == "Ąą�&lt;valve>�ą ZA</valve>WÓR ĄąĄą"       // WTF??
$test[4]['output'] == "テ�<valve>��ト </valve>ZAWÓR テスト"    // WTF??

解决方案https://drupal.org/node/1107268不会改变任何东西。

4

4 回答 4

2

我不确定为什么mb_stripos功能不起作用,但解决方法如下,

$str = mb_convert_case("ęóąśłżźćń", MB_CASE_UPPER, "UTF-8");
$result = mb_strrichr($str,"ĘÓĄŚŁŻŹĆŃ");
var_dump($result);

演示

于 2014-05-14T11:08:30.327 回答
2

当告诉您的字符串采用什么编码时,该函数可以正常工作:

var_dump(mb_stripos("ĘÓĄŚŁŻŹĆŃ",'ęóąśłżźćń', 0, 'UTF-8'));  // 0
                                                ^^^^^^^

如果没有显式编码参数,它可能会假定编码错误并且无法正确处理您的字符串。


您的测试代码的问题在于您将基于字符的索引与基于字节偏移的索引混合在一起。mb_strpos返回字符中的偏移量,同时substr_replace使用字节偏移量。在此处阅读有关该主题的内容:每个程序员都绝对需要了解的有关编码和字符集以使用文本的知识。

如果您想将某个单词包装在多字节字符串中的标签中,我宁愿建议这种方法:

preg_replace('/zawór/iu', '<valve>$0</valve>', $text)

请注意,$text 必须是 UTF-8 编码的,/u正则表达式仅适用于 UTF-8。

于 2014-05-14T11:43:22.300 回答
1

亲爱的 Rikesh,我用你的小费写道:

function patched_mb_stripos($content,$search) {
    $content=mb_convert_case($content, MB_CASE_LOWER, "UTF-8");
    $search=mb_convert_case($search, MB_CASE_LOWER, "UTF-8");
    return mb_stripos($content,$search);
}

它似乎工作:)

于 2014-05-14T11:38:32.843 回答
0

来自 https://gist.github.com/stemar/8287074的解决方案:

function mb_substr_replace($string, $replacement, $start, $length=NULL) {
if (is_array($string)) {
$num = count($string);
// $replacement
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
// $start
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value)
$start[$key] = is_int($value) ? $value : 0;
}
else {
$start = array_pad(array($start), $num, $start);
}
// $length
if (!isset($length)) {
$length = array_fill(0, $num, 0);
}
elseif (is_array($length)) {
$length = array_slice($length, 0, $num);
foreach ($length as $key => $value)
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
}
else {
$length = array_pad(array($length), $num, $length);
}
// Recursive call
return array_map(__FUNCTION__, $string, $replacement, $start, $length);
}
preg_match_all('/./us', (string)$string, $smatches);
preg_match_all('/./us', (string)$replacement, $rmatches);
if ($length === NULL) $length = mb_strlen($string);
array_splice($smatches[0], $start, $length, $rmatches[0]);
return join("",$smatches[0]);
}

用函数 test() 解决问题

于 2014-05-14T13:14:34.973 回答