PHP-REGEX: accented letters matches non-accented ones, and vice versa. How to achieve this?

妖精的绣舞 提交于 2019-11-29 11:34:48

You can try to make a function to create your regex expression based on your txt_search, replacing any possible match to all possible matches like this:

function search_term($txt_search) {
    $search = preg_quote($txt_search);

    $search = preg_replace('/[aàáâãåäæ]/iu', '[aàáâãåäæ]', $search);
    $search = preg_replace('/[eèéêë]/iu', '[eèéêë]', $search);
    $search = preg_replace('/[iìíîï]/iu', '[iìíîï]', $search);
    $search = preg_replace('/[oòóôõöø]/iu', '[oòóôõöø]', $search);
    $search = preg_replace('/[uùúûü]/iu', '[uùúûü]', $search);
    // add any other character

    return $search;
}

Then you use the result as a regex on your preg_replace.

You might have to parse the search string, and modify the pattern in the regex so that if includes cases like [eéÉ]. Replace all instances of e/E/é/É with a catch-all [eEéÉ]. Do the same for all other cases. So in your example the search pattern, instead of Jose/José/JOSÉ, would be jos[éÉeE]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!