PHP

Calculate the similarity of text

About this script

This PHP script generates an HTML table that compares a predefined name ("Luan") to a list of other names using string similarity.

Name Percentage
Brian 44%
Juan 75%
Leonardo 33%
Liam 50%
Logan 67%
Luan 100%
Mason 44%
Owen 25%
Sebastian 31%
William 18%
PHP
<table id="demoTable" class="table table-striped table-responsive">
  <thead>
  <tr>
  <th>Name</th>
  <th">Percentage</th>
  </tr>
  </thead>
  <tbody>
  <?php
  $nameToCompare = "Luan";
  $names = ['Brian', 'Juan', 'Leonardo', 'Liam', 'Logan', 'Luan', 'Mason', 'Owen', 'Sebastian', 'William'];

foreach ($names as $name) {
  similar_text($name, $nameToCompare, $similarity);
  $safeName = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
  $roundedSimilarity = round($similarity);
  echo "<tr>
  <td>{$safeName}</td>
  <td'>{$roundedSimilarity}%</td>
  </tr>";
  }
  ?>
  </tbody>
  </table>