Search code examples
phpregexpreg-match

Confusion regarding preg_match


So I'm trying to check if $oneAya is inside the string "هُوَ الْمَلِكُ الْقُدُّوسُ" but whenever I try the code below, it prints "not found".. Am I doing something wrong?

$oneAya = "هُوَ";
if(preg_match($oneAya,"هُوَ الْمَلِكُ الْقُدُّوسُ")) {
    echo 'found';
} else {
    echo 'not found';
}

Solution

  • First of all, your regex is not prepared thoroughly: you didn't put delimiters.

    $oneAya = "/هُوَ/";
    if(preg_match($oneAya,"هُوَ الْمَلِكُ الْقُدُّوسُ")) {
        echo 'found';
    } else {
        echo 'not found';
    }
    

    Secondly, if your PCRE is compiled with UTF-8 then you are fine, however preg_match() does much on solving your problem, simply you are in need of mb_strpos():

    if (mb_strpos("هُوَ الْمَلِكُ الْقُدُّوسُ", "هُوَ") !== false) {
        echo 'Found.';
    }