Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare two name string php

I am having a problem with comparing name strings. I have 3 variables

$fullname  = 'MASNAD HOSSAIN NEHITH';
$firstName = 'Masnad';
$LastName  = 'Nehith';

$fullname2  = 'MÄSNAD HOSSAIN NEHITH';
$firstName2 = 'Mäsnad';
$LastName2  = 'Nehith';

I thought of using strpos to see if the first name exists in the full name, but strpos is case sensitive.

I tried regular expressions using pregmatch but I am not sure how it works.

$pregmatch = preg_match("/$fullname/", $firstName);
if($pregmatch){
   echo " it matches";
 }
$pregmatch2 = preg_match("/$fullname2/", $firstName2);
if($pregmatch2){
   echo " it matches";
 }
like image 260
Masnad Nihit Avatar asked Jul 22 '26 02:07

Masnad Nihit


1 Answers

You need to use mb_stripos instead of simple stripos for UTF-8 characters like as

if(mb_stripos('MÄSNAD HOSSAIN NEHITH', 'Mäsnad') !== false)
{
    echo "UTF - 8 string".PHP_EOL;
}

if(mb_stripos('MASNAD HOSSAIN NEHITH', 'Masnad') !== false)
{
    echo "Normal String";
}

Output

UTF - 8 string
Normal String

Demo

like image 53
devmyb Avatar answered Jul 24 '26 18:07

devmyb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!