Matching php to regex possible

I found many questions about this, but none of them helped me with my particular problem. Situation: I want to look stringwith something "blablebli"and be able to find a match with all the accented variations of the ( "blablebli", "blábleblí", "blâblèbli"etc.) in the text.

I already made a workaround to the opposite (find the word without possible accents that I wrote). But I can’t understand how to realize what I want.

Here is my working code. (the corresponding part, this was part of the foreach, so we see only one word search):

$word="something";
$word = preg_quote(trim($word)); //Just in case
$word2 = $this->removeAccents($word); // Removed all accents
if(!empty($word)) {
    $sentence = "/(".$word.")|(".$word2.")/ui"; // Now I'm checking with and without accents.
    if (preg_match($sentence, $content)){
        echo "found";
    }
}

And my function removeAccents()(I'm not sure that I have touched on all possible accents with this preg_replace(). While this works. I would be grateful if someone checks that I missed something):

function removeAccents($string)
{
    return preg_replace('/[\`\~\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $string));
}

:

  • , $word a [aàáãâä] , ... litle .
  • , removeAccents() if $content , - :

    if (preg_match($sentence, $content) || preg_match($sentence, removeAccents($content)))
    

, , . $content.

preg_match() ? ?

+4
2

, , , . @CasimiretHippolyte , , .

, ( ):

function removeAccents($string)
{
    return preg_replace('/[\x{0300}-\x{036f}]/u', '', Normalizer::normalize($string, Normalizer::FORM_KD));
}

function addAccents($string)
{
    $array1 = array('a', 'c', 'e', 'i' , 'n', 'o', 'u', 'y');
    $array2 = array('[aàáâãäå]','[cçćĉċč]','[eèéêë]','[iìíîï]','[nñ]','[oòóôõö]','[uùúûü]','[yýÿ]');

    return str_replace($array1, $array2, strtolower($string));
}

$word="something";
$word = preg_quote(trim($word)); //Just in case
$word2 = $this->addAccents($this->removeAccents($word)); //check all possible accents
if(!empty($word)) {
    $sentence = "/(".$word.")|(".$word2.")/ui"; // Now I'm checking my normal word and the possible variations of it.
    if (preg_match($sentence, $content)){
        echo "found";
    }
}

Btw, im ( ). , addAccents() .

0

, , - :

<?php

// Convert unicode input to NFKD form.
$str = Normalizer::normalize("blábleblí", Normalizer::FORM_KD);

// Remove all combining characters (https://en.wikipedia.org/wiki/Combining_character).
var_dump(preg_replace('/[\x{0300}-\x{036f}]/u', "", $str));
+2

Source: https://habr.com/ru/post/1599649/


All Articles