Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:38:43+00:00 2026-06-01T23:38:43+00:00

I have an interface where a user will enter the name of a company.

  • 0

I have an interface where a user will enter the name of a company. It then compares what they typed to current entries in the database, and if something similar is found it presents them with options (in case they misspelled) or they can click a button which confirms what they typed is definitely new and unique.

The problem I am having is that it is not very accurate and often brings up dozens of “similar” matches that aren’t that similar at all!

Here is what I have now, the first large function I didn’t make and I am not clear on what exactly it does. Is there are much simpler way to acheive what I want?

// Compares strings and determines how similar they are based on a nth letter split     comparison.
function cmp_by_optionNumber($b, $a) {
if ($a["score"] == $b["score"]) return 0;
if ($a["score"] > $b["score"]) return 1;
return -1;
}
function string_compare($str_a, $str_b)
{
$length = strlen($str_a);
$length_b = strlen($str_b);

$i = 0;
$segmentcount = 0;
$segmentsinfo = array();
$segment = '';
while ($i < $length)
{
    $char = substr($str_a, $i, 1);
    if (strpos($str_b, $char) !== FALSE)
    {
        $segment = $segment.$char;
        if (strpos($str_b, $segment) !== FALSE)
        {
            $segmentpos_a = $i - strlen($segment) + 1;
            $segmentpos_b = strpos($str_b, $segment);
            $positiondiff = abs($segmentpos_a - $segmentpos_b);
            $posfactor = ($length - $positiondiff) / $length_b; // <-- ?
            $lengthfactor = strlen($segment)/$length;
           $segmentsinfo[$segmentcount] = array( 'segment' => $segment, 'score' => ($posfactor * $lengthfactor));
        }
        else
        {
             $segment = '';
             $i--;
             $segmentcount++;
         }
     }
     else
     {
         $segment = '';
        $segmentcount++;
     }
     $i++;
 }

 // PHP 5.3 lambda in array_map
 $totalscore = array_sum(array_map(function($v) { return $v['score'];  },    $segmentsinfo));
 return $totalscore;
}

$q = $_POST['stringA'] ;
$qLengthMin = strlen($q) - 5 ; // Part of search calibration. Smaller number = stricter.
$qLengthMax = strlen($q) + 2 ; // not in use.

$main = array() ;

include("pdoconnect.php") ;

$result = $dbh->query("SELECT id, name FROM entity_details WHERE
                  name LIKE '{$q[0]}%'
                  AND CHAR_LENGTH(name) >= '$qLengthMin'
                  #LIMIT 50") ; // The first letter MUST be correct.     This assumption makes checker faster and reduces irrelivant results.
$x = 0 ;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {

$percent = string_compare(strtolower($q), strtolower(rawurldecode($row['name']))) ;

if($percent == 1) {
    //echo 1 ;// 1 signifies an exact match on a company already in our DB.
    echo $row['id'] ;
    exit() ;
}
elseif($percent >= 0.6) { // Part of search calibration. Higher deci number = stricter.
    $x++ ;
    $main[$x]['name'] = rawurldecode($row['name']) ;
    $main[$x]['score'] = round($percent, 2) * 100;

    //array_push($overs, urldecode($row['name']) . " ($percent)<br />") ;
}

}

usort($main, "cmp_by_optionNumber") ;
$z = 0 ;
echo '<div style="overflow-y:scroll;height:175px;width:460px;">' ;
foreach($main as $c) {
if($c['score'] > 100) $c['score'] = 100 ;
if(count($main) > 1) {
echo '<div id="anysuggested' . $z . '" class="hoverdiv" onclick="selectAuto(' . "'score$z'" . ');">' ;
}
else echo '<div id="anysuggested' . $z . '" class="hoverdiv" style="color:#009444;" onclick="selectAuto(' . "'score$z'" . ');">' ;
echo '<span id="autoscore' . $z . '">' . $c['name'] . '</span></div>' ;
$z++ ;
}
echo '</div>' ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:38:44+00:00Added an answer on June 1, 2026 at 11:38 pm

    Comparing strings is a huge topic and there are many ways to do it. One very common algorithm is called the Levenshtein difference. This is a native implementation in PHP but none in MySQL. There is however an implementation here that you could use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A lot of JavaScript libraries have user interface widgets. Usually they are created after
In the user interface, i have a text field to enter date in dd/mm/yyyy
I want to create a service that will have no user interface and always
I have a User Interface where users can type in first name and last
I have to develop a web interface allowing the user to enter some inputs
I have been developing an user interface with Java Swing, and I have met
I have to make a graphical user interface application using the language of my
I always have trouble designing the user interface when it come to manage a
I have an interface like this: public interface IUser{ //some properties here T ToDerived(User
I have a bunch of XML files I'm using for user interface and string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.