By user2063756


2014-07-01 07:07:11 8 Comments

I have two strings

String 1: "sebastien"

String 2: "Sébastien"

I want to compare these two strings by ignoring é (Accents) character. Can anyone know this logic?

Thanks in advance

5 comments

@Soullivaneuh 2017-04-10 15:34:51

You can also use behat/transliterator package:

require_once 'vendor/autoload.php';

$stringWithoutAccent = 'acces';
$stringWithAccent = 'accès';

// true
var_dump($stringWithoutAccent === \Behat\Transliterator\Transliterator::unaccent($stringWithAccent));

@Rajesh J Nair 2014-07-01 07:17:11

<?php
$string_1 = 'sebastien';
$string_2 = 'Sébastien';

function transliterateString($txt) {
$transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'e', 'ё' => 'e', 'Ё' => 'e', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
$txt = str_replace(array_keys($transliterationTable),           array_values($transliterationTable), $txt);
return trim(strtolower($txt));
}
if(strcmp(transliterateString($string_1),transliterateString($string_2)) == 0)
{
echo 'strings are same';
}
else
{

echo 'Not Same';
}

?>

@ka_lin 2014-07-01 07:14:42

Use Normalizer PHP extension or iconv, more info:

http://www.php.net/manual/en/class.normalizer.php
http://www.php.net/manual/en/function.iconv.php

<?php
$string = 'Sébastien';
echo Normalizer::normalize($string);
echo iconv('UTF-8', 'ASCII//TRANSLIT', $string);
?>

@Vivek Nanda 2014-07-01 07:13:58

You can simply decode the strings in utf8 and compare them.

Otherwise, if by ignore, you meant not compare the character at all, then you might have to do a char-by-char comparison. First, get both strings to upper or lower case, and wherever you come across a non unicode character, skip the comparison and move to comparing the next char.

@A-312 2014-07-01 07:12:07

Calculate the similarity between two strings with similar_text function.

Before parse "é" with :

<?php
function normalize($string) {
    $string = strtr($string, array('ő' => 'o', 'ű' => 'u', 'Ő' => 'O', 'Ű' => 'U'));
    $a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖŐØÙÚÛÜŰÝÞßàáâãäåæçèéêëìíîïðñòóôõöőøùúûűüýýþÿŔŕ';
    $b = 'AAAAAAACEEEEIIIIDNOOOOOOOUUUUUYbsaaaaaaaceeeeiiiidnooooooouuuuuyybyRr';
    $string = utf8_decode($string);   
    $string = strtr($string, utf8_decode($a), $b);
    return utf8_encode($string);
}
?>

See more at: http://bitprison.net/php_normalize_string_function#sthash.tMoMbaOG.dpuf

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] How do I properly compare strings?

  • 2011-11-04 02:22:27
  • nmagerko
  • 414735 View
  • 170 Score
  • 8 Answer
  • Tags:   c string strcmp

37 Answered Questions

[SOLVED] PHP and Enumerations

  • 2008-10-31 18:51:14
  • Henrik Paul
  • 471403 View
  • 1121 Score
  • 37 Answer
  • Tags:   php enumeration

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

14 Answered Questions

[SOLVED] Secure hash and salt for PHP passwords

11 Answered Questions

[SOLVED] How to get the last char of a string in PHP?

  • 2010-04-21 09:44:57
  • streetparade
  • 378016 View
  • 454 Score
  • 11 Answer
  • Tags:   php string

26 Answered Questions

[SOLVED] PHP array delete by value (not key)

  • 2011-08-29 00:44:04
  • Adam Strudwick
  • 786741 View
  • 841 Score
  • 26 Answer
  • Tags:   php arrays

36 Answered Questions

[SOLVED] How do I check if a string contains a specific word?

31 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 813803 View
  • 1427 Score
  • 31 Answer
  • Tags:   php string

7 Answered Questions

[SOLVED] How do I compare two DateTime objects in PHP 5.2.8?

  • 2009-06-07 02:33:23
  • RedBlueThing
  • 297026 View
  • 288 Score
  • 7 Answer
  • Tags:   php datetime

2 Answered Questions

[SOLVED] how to compare case insensitive two strings in php

  • 2011-07-25 07:51:19
  • SureshKumar Vegesna
  • 846 View
  • 0 Score
  • 2 Answer
  • Tags:   php

Sponsored Content