preg_match() fails with string containing slashes

后端 未结 1 930
甜味超标
甜味超标 2021-01-24 14:12

I have a function like this:

function in_array_r($item , $array){
        return preg_match(\'/\"\'.$item.\'\"/i\' , json_encode($array));
}

an

1条回答
  •  -上瘾入骨i
    2021-01-24 15:06

    This is because the slash in your input: blah / blah / something is seen as delimiter for the regex.

    To solve this you can escape your input with preg_quote(), e.g.

    function in_array_r($item , $array){
        return preg_match('/"'. preg_quote($item, "/") .'"/i' , json_encode($array));
    }

    0 讨论(0)
提交回复
热议问题