Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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中的mb#u stripos()赢得';我不能正常工作_Php_Stripos - Fatal编程技术网

PHP中的mb#u stripos()赢得';我不能正常工作

PHP中的mb#u stripos()赢得';我不能正常工作,php,stripos,Php,Stripos,此代码: setlocale(LC_ALL, 'pl_PL', 'pl', 'Polish_Poland.28592'); $result = mb_stripos("ĘÓĄŚŁŻŹĆŃ",'ęóąśłżźćń'); 返回false 如何解决这个问题 这不是正确的答案 更新:我做了一个测试: function test() { $search = "zawór"; $searchlen=strlen($search); $opentag="<valve>"; $o

此代码:

setlocale(LC_ALL, 'pl_PL', 'pl', 'Polish_Poland.28592');
$result = mb_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;
}
功能测试(){
$search=“zawór”;$searchlen=strlen($search);
$opentag=“”;$opentaglen=strlen($opentag);
$closetag=“”;$closetaglen=strlen($closetag);
$test[0]['input']=“test ZAWÓR test”;//正常测试
$test[1]['input']=“X\nX\nX ZAWÓR X\nX\nX”;//白字符测试
$test[2]['input']=“
ZAWÓR
”;//html换行测试 $test[3][input']=“波兰语变调测试” $test[4]['input']=”テスト 扎乌尔テスト"; //日本片假名试验 foreach($testas$key=>$val){ $position=mb_stripos($val['input'],$search,0,'UTF-8'); 如果($position!=false){ $output=$val['input']; $output=substr\u replace($output,$opentag,$position,0); $output=substr\u replace($output,$closetag,$position+$opentaglen+$searchlen,0); $test[$key]['output']=$output; } 否则{ $test[$key]['output']=null; } } 返回$test; }
FIREFOX输出:

$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'] == "Ąą�<valve>�ą ZA</valve>WÓR ĄąĄą"       // WTF??
$test[4]['output'] == "テ�<valve>��ト </valve>ZAWÓR テスト"    // WTF??
$test[0]['output']==“test ZAWÓR test”//ok
$test[1]['output']==“X\nX\nX ZAWÓR X\nX\nX”//ok
$test[2]['output']=“
ZAWÓR
”//确定 $test[3]['output']==“Ą��ਲ਼ZAWਲ਼Rਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼/WTF?? $test[4]['output']=”テ���ト 扎乌尔テスト“//WTF??

解决方案不会改变任何东西。

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

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

亲爱的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);
}
而且它似乎是有效的:)

当被告知字符串的编码方式时,函数:

var_dump(mb_stripos("ĘÓĄŚŁŻŹĆŃ",'ęóąśłżźćń', 0, 'UTF-8'));  // 0
                                                ^^^^^^^
如果没有显式编码参数,它可能会假定编码错误,并且无法正确处理字符串


测试代码的问题在于,您将基于字符的索引与基于字节偏移量的索引混合使用。
mb_strpos
以字符为单位返回偏移量,而
substr\u replace
使用字节偏移量。请阅读此处的主题:

如果您想将标签中的某个单词包装成多字节字符串,我建议您采用以下方法:

preg_replace('/zawór/iu', '<valve>$0</valve>', $text)
preg_replace('/zawór/iu','$0',$text)
请注意,
$text
必须是UTF-8编码,
/u
正则表达式仅适用于UTF-8。

解决方案来自:


解决了function test()的问题。

请参阅我的更新答案:您的更新问题。我认为在这种情况下,function substr\u replace有问题,但我已经在上找到了一个小补丁。但是您的想法似乎也不错:)我必须尝试一下:)
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]);
}